Skip to content
Guess outputAdvanced5 questions

Arrays, references and generators: guess the output

Array union, lingering references, generators and late static binding.

  1. 01What does this print?
    <?php
    
    $a = [1, 2];
    $b = [3, 4, 5];
    
    echo implode(",", $a + $b), " | ", implode(",", array_merge($a, $b));
  2. 02What does this print?
    <?php
    
    $items = [1, 2, 3];
    
    foreach ($items as &$item) {
    }
    
    foreach ($items as $item) {
    }
    
    echo implode(",", $items);
  3. 03What does this print?
    <?php
    
    function gen()
    {
        echo "A";
        yield 1;
        echo "B";
        yield 2;
        echo "C";
    }
    
    foreach (gen() as $value) {
        echo $value;
    }
  4. 04What does this print?
    <?php
    
    class Model
    {
        public static function viaStatic(): static
        {
            return new static;
        }
    
        public static function viaSelf(): self
        {
            return new self;
        }
    }
    
    class User extends Model {}
    
    echo get_class(User::viaStatic()), " ", get_class(User::viaSelf());
  5. 05What does this print?
    <?php
    
    $data = ["key" => null];
    
    echo isset($data["key"]) ? "set" : "not set";
    echo " / ";
    echo array_key_exists("key", $data) ? "exists" : "missing";

Answers are scored on the server. Unanswered questions count as incorrect.

laravel.now is an independent community project. It is not affiliated with, endorsed by, sponsored by, or in any way officially connected to Laravel, Laravel LLC, Taylor Otwell, or the Laravel framework maintainers. Laravel is a trademark of Taylor Otwell. All other trademarks belong to their respective owners.

This site is operated by the makers of Lockhound. Links to Lockhound are promotional.