PHPitfalls

 PHPitfalls

PHP is known for it's quirks and we are pretty sure most of us have cursed some of the inconsistencies and weird behaviors we have encountered in our day to day work. In this game we want to shed some light on common (and not so common) pitfalls.

The game was presented and created with @dbrumann, @derrabus, @xabbuh and beer.

0bee6a2886272e60be8888ae48baf42d?s=128

Christopher Hertel

August 23, 2017
Tweet

Transcript

  1. PHPitfalls
 Symfony User Group Berlin

  2. None
  3. RULES

  4. Rules ▸ 3 Rounds ▸ Qualification Round ▸ Main Round

    ▸ Final Round ▸ Runtime is PHP 7.0/7.1 ▸ Don’t cheat! (no mobile devices) ▸ Be fair
  5. LET’S START

  6. ROUND 1 QUALIFICATION

  7. Round 1 - Qualification ▸ Take a quiz ▸ Take

    a pen ▸ Answer the 5 questions (single choice) ▸ Hand the quiz to us ▸ Have a beer & wait for the next round
  8. None
  9. QUESTION 1 What is the output of this code snippet?

    $a = 4; echo $a++ + $a++; 8 A 10 C 11 D 9 B
  10. QUESTION 1 What is the output of this code snippet?

    $a = 4; echo $a++ + $a++; 8 A 10 C 11 D 9 B
  11. QUESTION 2 What is the output of this code snippet?

    var_dump(true ? 'Foo' : false ? 'Bar' : 'Baz'); string(3) Foo A bool(false) C string(3) Baz D string(3) Bar B
  12. QUESTION 2 What is the output of this code snippet?

    var_dump(true ? 'Foo' : false ? 'Bar' : 'Baz'); string(3) Foo A bool(false) C string(3) Baz D string(3) Bar B
  13. QUESTION 3 What is the output of this code snippet?

    $x = 0; echo 'Hello ' . $x+42 . ' World'; Fatal Error A Hello 43 C Hello 42 World D 42 World B
  14. QUESTION 3 What is the output of this code snippet?

    $x = 0; echo 'Hello ' . $x+42 . ' World'; Fatal Error A Hello 43 C Hello 42 World D 42 World B
  15. QUESTION 4 What is the output of this code snippet?

    $versions = ['7.1', '7.2', '7.3']; var_dump(in_array('7.10', $versions)); bool(true) A bool(false) B
  16. QUESTION 4 What is the output of this code snippet?

    $versions = ['7.1', '7.2', '7.3']; var_dump(in_array('7.10', $versions)); bool(true) A bool(false) B
  17. QUESTION 5 Which array key will be overridden? 0234 A

    '0' B 'test' C 234 D $array = [ 'test' => 'foo', '0' => 'baz', 0234 => 'x', 234 => 'y', '0234' => 'bar', '234' => 'omg', '0.0' => 'yeah' ];
  18. QUESTION 5 Which array key will be overridden? 0234 A

    '0' B 'test' C 234 D $array = [ 'test' => 'foo', '0' => 'baz', 0234 => 'x', 234 => 'y', '0234' => 'bar', '234' => 'omg', '0.0' => 'yeah' ];
  19. ROUND 2 MAIN ROUND

  20. QUESTION 6 What is the output of this code snippet?

    echo(print('hello')); hellohello A hello1 C empty output D hello B
  21. QUESTION 6 What is the output of this code snippet?

    echo(print('hello')); hellohello A hello1 C empty output D hello B
  22. QUESTION 7 What is the output of this code snippet?

    $max = (int)(float)PHP_INT_MAX; var_dump($max + PHP_INT_MAX); int(-1) A int(-9223372036854775808) C int(0) D double(1.844674407371E+19) B
  23. QUESTION 7 What is the output of this code snippet?

    $max = (int)(float)PHP_INT_MAX; var_dump($max + PHP_INT_MAX); int(-1) A int(-9223372036854775808) C int(0) D double(1.844674407371E+19) B
  24. QUESTION 8 What is the output of this code snippet?

    list($a, $a) = array(1, 2, 3, 4); var_dump($a); Fatal error A int(1) C int(2) D int(4) B
  25. QUESTION 8 What is the output of this code snippet?

    list($a, $a) = array(1, 2, 3, 4); var_dump($a); Fatal error A int(1) C int(2) D int(4) B BTW: BC BREAK OF PHP 7
  26. QUESTION 9 What is the output of this code snippet?

    $array = [ 5076964154930102272 => 2, 999999999999999999999999999999 => 4, ]; var_dump(array_sum($array)); int(6) A int(2) C int(-2) D int(4) B
  27. QUESTION 9 What is the output of this code snippet?

    $array = [ 5076964154930102272 => 2, 999999999999999999999999999999 => 4, ]; var_dump(array_sum($array)); int(6) A int(2) C int(-2) D int(4) B
  28. QUESTION 10 What is the output of this code snippet?

    $array = [1, 2, 3]; foreach ($array as &$v) {} foreach ($array as $v) {} echo implode(',', $array); 1,1,1 A 1,2,2 C 3,3,3 D 1,2,3 B
  29. QUESTION 10 What is the output of this code snippet?

    $array = [1, 2, 3]; foreach ($array as &$v) {} foreach ($array as $v) {} echo implode(',', $array); 1,1,1 A 1,2,2 C 3,3,3 D 1,2,3 B
  30. QUESTION 11 What is the output of this code snippet?

    function compare($a, $b, $c) { echo (int)($a < $b); echo (int)($b < $c); echo (int)($c < $a); } compare('066', '07x', '28'); 010 A 111 C 001 D 110 B
  31. QUESTION 11 What is the output of this code snippet?

    function compare($a, $b, $c) { echo (int)($a < $b); echo (int)($b < $c); echo (int)($c < $a); } compare('066', '07x', '28'); 010 A 111 C 001 D 110 B
  32. QUESTION 12 What is the output of this code snippet?

    namespace Foo { class Bar { } } namespace { echo(Bar::class); } Bar A Fatal Error C empty output D Foo\Bar B
  33. QUESTION 12 What is the output of this code snippet?

    namespace Foo { class Bar { } } namespace { echo(Bar::class); } Bar A Fatal Error C empty output D Foo\Bar B
  34. QUESTION 13 What is the output of this code snippet?

    function foo() { try { return 1337; } finally { return 42; } } var_dump(foo()); int(1337) A string(6) 133742 C int(42) D Fatal Error B
  35. QUESTION 13 What is the output of this code snippet?

    function foo() { try { return 1337; } finally { return 42; } } var_dump(foo()); int(1337) A string(6) 133742 C int(42) D Fatal Error B
  36. QUESTION 14 What is the output of this code snippet?

    $nan = NAN; var_dump(0 < NAN); var_dump(0 < $nan); bool(true) bool(true) A bool(true) bool(false) C bool(false) bool(false) D bool(false) bool(true) B
  37. QUESTION 14 What is the output of this code snippet?

    $nan = NAN; var_dump(0 < NAN); var_dump(0 < $nan); bool(true) bool(true) A bool(true) bool(false) C bool(false) bool(false) D bool(false) bool(true) B IT’S A BUG - FIXED WITH 7.2
  38. QUESTION 15 What is the output of this code snippet?

    $objects = [new \stdClass()]; var_dump(in_array((object)[], $objects)); bool(true) A bool(false) B
  39. QUESTION 15 What is the output of this code snippet?

    $objects = [new \stdClass()]; var_dump(in_array((object)[], $objects)); bool(true) A bool(false) B
  40. ROUND 3 FINAL

  41. QUESTION 16 What is the output of this code snippet?

    echo(printf('hello'));
  42. QUESTION 16 What is the output of this code snippet?

    echo(printf('hello')); hello5
  43. QUESTION 17 What is the output of this code snippet?

    $date1 = new DateTimeImmutable( '2017-10-29', new DateTimeZone('Europe/Berlin') ); $date2 = $date1->modify('+2 hours'); echo $date1->diff($date2)->format('%h');
  44. QUESTION 17 What is the output of this code snippet?

    $date1 = new DateTimeImmutable( '2017-10-29', new DateTimeZone('Europe/Berlin') ); $date2 = $date1->modify('+2 hours'); echo $date1->diff($date2)->format('%h'); 3
  45. QUESTION 18 Which number is displayed? use const true as

    false; if (false) { echo 1; } if (true) { echo 2; } if (true === false) { echo 3; } if (\false) { echo 4; } if (\true) { echo 5; } if (2 > 1) { echo 6; }
  46. QUESTION 18 Which number is displayed? use const true as

    false; if (false) { echo 1; } if (true) { echo 2; } if (true === false) { echo 3; } if (\false) { echo 4; } if (\true) { echo 5; } if (2 > 1) { echo 6; } 12356
  47. QUESTION 19 Which string is displayed? class Foo { public

    $bar = 'baz'; static function create() { $foo = new self(); $foo->bar .= 'baz'; $foo->foo(); return $foo; } private function foo() { $this->bar .= 'bar'; } } $foo = Foo::create(); echo $foo->bar;
  48. QUESTION 19 Which string is displayed? bazbarbazbar class Foo {

    public $bar = 'baz'; static function create() { $foo = new self(); $foo->bar .= 'baz'; $foo->foo(); return $foo; } private function foo() { $this->bar .= 'bar'; } } $foo = Foo::create(); echo $foo->bar;
  49. QUESTION 20 What is the output of file2.php? // file1.php

    $a = 1; $b = 1; var_dump($a + $b); // file2.php $b = new class { function __destruct(){ $GLOBALS['b'] = 2; } }; require 'file1.php'; Credits: Nikita Popov - Static Optimization of PHP Bytecode
  50. QUESTION 20 What is the output of file2.php? // file1.php

    $a = 1; $b = 1; var_dump($a + $b); // file2.php $b = new class { function __destruct(){ $GLOBALS['b'] = 2; } }; require 'file1.php'; int(3) Credits: Nikita Popov - Static Optimization of PHP Bytecode
  51. GAME OVER

  52. THANKS