Friday's PHP quiz: some Brad programmer's adventures, one weird sequence and prizes

Hello! They promised - we do: we continue the series of mini-quizzes dedicated to different programming languages ​​in our blog (previous: 1 (for knowledge of Python, PHP, Golang and DevOps) , 2 (fully Go) ). Today's edition is dedicated to PHP.

Under the cut - eight questions, some adventures of the programmer Brad, one strange sequence and cool merchandise as prizes. Quiz runs until July 4.

UPD 2: As agreed, we post the quiz quest analysis. Explanations hid under the spoiler after the correct answers. If you have any questions, ask them in the comments.

UPD: We've finished accepting answers. Thanks to everyone who participated! We are preparing an analysis of tasks. The answers to them are inside the text, and the winners and prize-winners are under the spoiler.
Winners and prize-winners of the PHP quiz

Winner


egor_nullptr

Prize winners


We randomly selected ten best participants who made no more than two errors in the answers: Dimd13 , slimus , alexchromets , Donquih0te , TexElless , SamDark , AdmAlexus , voiceofnoise , Raz-Mik , Serj_By .

Recording a draw


Bonus!


For those who have made only one mistake, we additionally donate holivarny bones, with which you can decide in which backend language and the frontend framework to write your new project / or redo the old one. They receive them: DjSebas , TexElless , Turik-us , offlinewan , voiceofnoise , andrey_96 , delight-almighty .




Rules of the game


To the first person who answers them correctly, we’ll send Avito's souvenir pack: a php-elephant T-shirt, socks and holivary bones (it will be possible to tell in which backend language and the frontend framework your new project will be written).

We will send Avito-socks to ten others who answer correctly . Let's play with randomizer. He will determine who will go to two more T-shirts and a set of bones.



Questions and answer choices


Question 1


What will output code:

<?php $a = [1, 2, 3]; foreach($a as &$value) {} foreach($a as $value) {} print_r($a); 

Variants of answers:

  1. Array (1, 2, 3)
  2. Array (1, 2, 2)
  3. Array (3, 2, 1)
  4. Mistake

Correct answer
Array (1, 2, 2)

Explanation
Since in php the variables created in the loop remain alive after its completion, by the beginning of the second cycle the variable $ value is a link to the last element of the array. During the last foreach iteration, the values ​​from the array are written to its last element (because $ value is a link). This is how the array will look like at each iteration of the second cycle:
1. [1, 2, 1]
2. [1, 2, 2]
3. [1, 2, 2]

Question 2


What will output code:

 <?php function sowCrops() { return 'wheat'; } function millWheat() { return 'flour'; } function bake($flour) { return 'cupcake'; } function generator() { $flour = yield millWheat(); $wheat = yield sowCrops(); return bake($flour); }; $gen = generator(); foreach ($gen as $key => $value) { echo $key . ' => ' . $value . PHP_EOL; } echo $gen->getReturn(); 

Variants of answers:

  1.   0 => flour 1 => wheat 

  2.   0 => wheat 1 => flour 2 => cupcake 

  3.  0 => flour 1 => wheat cupcake 

  4.  cupcake 



Correct answer
four.
 0 => flour 1 => wheat cupcake 



Explanation
No comments. Just a little tricky example with generators.

Question 3


Once a programmer Brad decided to port a single library from Go to PHP in order to collect stars on GitHub and wondered:

Is the following construction possible?

 <?php print_r(...(new Foo())); 

Variants of answers:

  1. Yes, the Foo class must implement the Traversable interface.
  2. Yes, the Foo class must implement the ArrayAccess interface methods.
  3. No, there will be an error, the argument of the ...- operator must be an array

Correct answer
1. Yes, the Foo class must implement the Traversable interface.

Explanation
It's simple. From the Argument Unpacking documentation :
Arrays and objects that implement the Traversable interface can be unpacked into an argument list when passed to a function using the operator ...


Question 4


What sorting algorithm is used in the heart of PHP for functions such as sort, etc.?

Variants of answers:

  1. non-recursive mergesort
  2. heapsort (Edsger Dijkstra's smoothsort variation)
  3. quicksort median of three
  4. introsort

Correct answer
4. introsort

Explanation
Introsort is used. Source code can be viewed on Github . There is a mention in the documentation about using quicksort, but there is no contradiction here, because introsort is a hybrid sorting algorithm, where with a small number of elements, insert sorting is used, and later a faster algorithm is used: quicksort or heapsort.


Question 5


There is a code:

 <?php class Factory { public function getLambda(): Closure { return function () { printf("Here I am (%s)!\n", get_class($this)); }; } public function getLambda2(): Closure { return static function () { printf("Here I am (%s)!\n", get_class($this)); }; } } 

Question: Is there a difference between the return values ​​of getLambda and getLambda2?

Variants of answers:

  1. In one case, the keyword static :) is used, but it does not affect
  2. The result of getLambda2 () cannot be bound to any object.
  3. So you can not write: there will be a syntax error "Syntax error: static keyword used in wrong context"
  4. The closure of getLamda2 () can bind (bindTo) only to classes

Correct answer
2. The result of getLambda2 () cannot be bound to any object.

Explanation
The getLambda2 () method returns a static anonymous function that cannot be bound to an object via the -> bindTo () method. Their use is rarely found in code, but still

Question 6


What will output code:

 <?php $a = true; $b = false; $c = $a and $b; $d = $a && $b; var_dump($c); var_dump($d); 

Variants of answers:

  1.  bool(false) bool(false) 
  2.  bool(false) bool(true) 
  3.  bool(true) bool(true) 
  4.  bool(true) bool(false) 

Correct answer
four.
 bool(true) bool(false) 


Explanation
The difference between && and and in priority. The expression $ d = $ a && $ b works like $ d = ($ a && $ b). But the expression $ c = $ a and $ b works differently and can be represented as (($ c = $ a) and $ b).

Question 7


What will output code:

 <?php $a = 'a'; for ($i = 0; $i < 40; $i++) { echo $a++, PHP_EOL; } 

Variants of answers:

  1. Numbers from 0 to 39 will be displayed, as well as Warning: A non-numeric value encountered in at each iteration
  2. Each iteration will display a '+'
  3. Strange sequence:
     a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an 

Correct answer
3. Strange sequence:
 a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an 


Explanation
Not everyone knows, but the increment operator can be applied to characters. From the documentation:
PHP follows Perl conventions (as opposed to C) to perform arithmetic operations on character variables. For example, in PHP and Perl $ a = 'Z'; $ a ++; assigns $ a the value 'AA', while in C a = 'Z'; a ++; will assign a to the value '[' (the ASCII value of 'Z' is 90, and the ASCII value of '[' is 91).


Question 8


What will output code:

 <?php class TestMe { public function test() { if (0) { yield 32332; } return [1,2,3]; } } $t = new TestMe(); foreach ($t->test() as $id) { echo $id, PHP_EOL; } echo "The end", PHP_EOL; 

Variants of answers:

  1.  1 2 3 32332 The end 

  2.  1 2 3 The end 

  3.  The end 

  4.  32332 The end 


Correct answer
3
 The end 



Explanation
At first glance it may seem that the function does not return a generator, because the yield expression is unreachable. However, any function containing a yield expression automatically becomes a generator function. It is written in the original RFC. At the time of the first iteration, the generator begins to execute the function code from the very beginning to the first available yield expression, but since it does not exist, the generator finishes its work without transferring any data to the loop.

Summarizing


Answers to questions will post an update to the post on Wednesday, July 4th . If you decide - put the answers under the spoiler, so as not to spoil the other fans. And do not forget to check the personal of Habr after the end of the quiz.

Enjoy!

Source: https://habr.com/ru/post/414481/


All Articles