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

phpitfalls - cologne

phpitfalls - cologne

David Badura

January 24, 2018
Tweet

More Decks by David Badura

Other Decks in Programming

Transcript

  1. HERE BE DRAGONS Copyright ▸ Symfony User Group Berlin ▸

    Alexander Turek ▸ Christopher Hertel ▸ Denis Brumann
  2. HERE COULD BE YOUR ADVERTISING Rules ▸ 3 Rounds ▸

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

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

    $a = 5; echo $a++ + $a++; 11 A 10 C 12 D 13 B
  6. QUESTION 2 What is the output of this code snippet?

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

    var_dump(true ? 'Foo' : false ? 'Bar' : 'Baz'); string(3) Foo A string(3) Bar C string(3) Baz D bool(false) 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 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
  10. QUESTION 4 Which array key will be overridden? '0' A

    0135 B 135 C $array = [ 'test' => 'foo', '0' => 'bar', 0135 => 'baz', 135 => 'x', '0135' => 'y', ’135’ => 'omg', '0.0' => 'yeah' ]; Nothing D
  11. QUESTION 4 Which array key will be overridden? '0' A

    0135 B 135 C Nothing D $array = [ 'test' => 'foo', '0' => 'bar', 0135 => 'baz', 135 => 'x', '0135' => 'y', ’135’ => 'omg', '0.0' => 'yeah' ];
  12. QUESTION 5 What is the output of this code snippet?

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

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

    echo(print('symfony')); symfonysymfony A symfony C symfony7 D symfony1 B
  15. QUESTION 6 What is the output of this code snippet?

    echo(print('symfony')); symfonysymfony A symfony C symfony7 D symfony1 B
  16. QUESTION 7 What is the output of this code snippet?

    $a = "1d9"; $a++; $a++; echo $a; 2 A 0 C fatal error D 1e1 B
  17. QUESTION 7 What is the output of this code snippet?

    $a = "1d9"; $a++; $a++; echo $a; 2 A 0 C fatal error D 1e1 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
  19. 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
  20. QUESTION 9 What is the output of this code snippet?

    $array = [ 9223372036854775807 => 6, 5076964154930102272 => 2, 999999999999999999999999999999 => 4, ]; var_dump(array_sum($array)); int(12) A int(8) C int(10) D int(6) B
  21. 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 $array = [ 9223372036854775807 => 6, 5076964154930102272 => 2, 999999999999999999999999999999 => 4, ]; var_dump(array_sum($array)); int(12) A int(8) C int(10) D int(6) B
  22. QUESTION 10 What is the output of this code snippet?

    $array = [1, 2, 3]; foreach ($array as &$v) {} foreach ($array as $v) {} var_dump(array_sum($array)); 1 A 5 C 6 D 9 B
  23. QUESTION 10 What is the output of this code snippet?

    $array = [1, 2, 3]; foreach ($array as &$v) {} foreach ($array as $v) {} var_dump(array_sum($array)); 1 A 5 C 6 D 9 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 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
  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 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
  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 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
  30. QUESTION 14 What is the output of this code snippet?

    $date = new \DateTimeImmutable('2018-01-24'); $date->__construct('2017-12-24'); echo $date->format('d.m.Y'); bool(true) A bool(false) B Fatal Error A 24.12.2017 C 24.01.2018 D 24.01.18 B
  31. QUESTION 14 What is the output of this code snippet?

    $date = new \DateTimeImmutable('2018-01-24'); $date->__construct('2017-12-24'); echo $date->format('d.m.Y'); bool(true) A bool(false) B Fatal Error A 24.12.2017 C 24.01.2018 D 24.01.18 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 fatal error C D segmentation fault
  33. 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 fatal error C D segmentation fault
  34. QUESTION 16 What is the output of this code snippet?

    echo(printf('hello world')); hello world11
  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 = 'foo'; static function create() { $foo = new self(); $foo->bar .= 'foo'; $foo->foo(); return $foo; } private function foo() { $this->bar .= 'bar'; } } $foo = Foo::create(); echo $foo->bar;
  38. QUESTION 19 Which string is displayed? foobarfoobar class Foo {

    public $bar = 'foo'; static function create() { $foo = new self(); $foo->bar .= 'foo'; $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? // include.php

    $a = 5; $b = 1; var_dump($a + $b); // main.php $b = new class { function __destruct(){ $GLOBALS['b'] = 5; } }; require 'include.php';
  40. QUESTION 20 What is the output of file2.php? // include.php

    $a = 5; $b = 1; var_dump($a + $b); // main.php $b = new class { function __destruct(){ $GLOBALS['b'] = 5; } }; require 'include.php'; int(10) Credits: Nikita Popov - Static Optimization of PHP Bytecode