Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Christopher Hertel

August 23, 2017
Tweet

More Decks by Christopher Hertel

Other Decks in Programming

Transcript

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

    ▸ Final Round ▸ Runtime is PHP 7.0/7.1 ▸ Don’t cheat! (no mobile devices) ▸ Be fair
  2. 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
  3. QUESTION 1 What is the output of this code snippet?

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

    $a = 4; echo $a++ + $a++; 8 A 10 C 11 D 9 B
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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' ];
  12. 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' ];
  13. QUESTION 6 What is the output of this code snippet?

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

    echo(print('hello')); hellohello A hello1 C empty output D hello B
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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');
  34. 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
  35. 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; }
  36. 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
  37. 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;
  38. 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;
  39. 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
  40. 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