Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Top 10 php classic traps php serbia

68 views

Published on

PHP has its own treasure chest of classic mistakes that surprises even the most seasoned expert: code that dies just by changing its namespace, strpos() that fails to find strings or arrays that changes without touching them. Do that get on your nerves too? Let's make a list of them, so we can always teach them to the new guys, spot them during code reviews and kick them out of our code once and for all. Come on, you're not frightening us!

Published in: Technology
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Top 10 php classic traps php serbia

  1. 1. Top 10 PHP classic traps PHP serbia, Belgrad, Serbia, 2019
  2. 2. Agenda hours 60 minutes Top 10 classic PHP traps Improve your code now This may wait for tomorrow… 321
  3. 3. WHO'S SPEAKING? ➤ Damien Seguy ➤ CTO at Exakat ➤ Static analysis tool ➤ Elephpant retirement home
  4. 4. A bug waiting in the code A performance potential A convenient tool The way of the elephpant Questions along the way 🐞 🛠 🐘 🚀 (When I want)
  5. 5. The legend of strpos() <?php if (strpos($string, 'a'))  { } if (strpos($string, 'a') == 0) { } if ($x = strpos($string, 'a')) { } 🐞
  6. 6. The real face of strpos() <?php // Only for comparison with 0/false if (strpos($string, 'a') === false) { } // No zero, no confusion if (strpos($string, 'a') == 2) { } // strpos() is not the only one... if (preg_match($regex, $string)) { } 🐘
  7. 7. strpos()-like array_search() collator_compare() collator_get_sort_key() current() fgetc() file_get_contents() file_put_contents() fread() iconv_strpos() iconv_strrpos() imagecolorallocate() imagecolorallocatealpha() mb_strlen() next() pcntl_getpriority() preg_match() prev() readdir() stripos() strpos() strripos() strrpos() strtok() curl_exec()
  8. 8. The other legend of strpos() <?php if (openssl_verify($data,  $signature,  $public_key)) {     login($user); } ?> 🐞 1 => success 0 => failure -1 => error => true => false => true
  9. 9. random_int() throws an exception openssl_random_pseu do_bytes() too, in PHP 7.4 openssl_verify()-like pcntl_wait ftp_size pg_field_num pg_set_client_encoding ldap_compare pcntl_waitpid event_base_loop openssl_pkcs7_verify openssl_x509_checkpurpose openssl_verify posix_setsid odbc_num_rows odbc_num_fields
  10. 10. Define() <?php  define('A', true); ?> 🚀
  11. 11. From define() to const <?php  const A = true; ?> 🐘
  12. 12. define() is on the way out <?php   define('A', 3, true);  define($x, $y);  ?> 🛠
  13. 13. Constant static expressions <?php  const A = true; const B = 33 + 12 * (23 - 34); const C = array(A, B, D::E); const D = A ? B : C; const E = [1] + C; ?> 🛠
  14. 14. Repeated print <?php   print 'a';   print $b ;   print 'c'; ?> 🚀
  15. 15. Repeated print <?php   print 'a' . $b . 'c'; ?> 🚀
  16. 16. Repeated print echo <?php   echo  'a' , $b , 'c'; ?> 🐘
  17. 17. Repeated echo <?php   echo  'a', $b , 'c'; ?>
  18. 18. echo doesn't "function" <?php   echo( 'a', $b , 'c', ); ?>
  19. 19. A reverse problem <?php $fp = fopen($file, 'w'); foreach($array as $row) {   fputcsv($fp, $row); } fclose($fp); ?> 🚀
  20. 20. Dump it like Beckam <?php $fp = fopen('php://memory', 'w+'); foreach($array as $row) {   fputcsv($fp, $row); } rewind($fp); file_put_contents($file,  stream_get_contents($fp)); ?> 🚀
  21. 21. <?php $b = 3; $c = 1; $a1 = $b and $c; $a2 = $b && $c; ?> 🐞 Logic, written in full $a1? = true false 0 1 2 3 $a2? = true false 0 1 2 3
  22. 22. Logic, written in full <?php $b = 3; $c = 1; $a1 = ($b and $c); $a2 = $b && $c; ?> 🐘 Operator precedence
  23. 23. Order matters <?php    $x = new stdClass(); var_dump(!$x instanceof stdClass); ?>
  24. 24. Order matters <?php     $a = 1; $b = 2; echo '$a + $b = ' . $a + $b; ?> 🐞 PHP 7.4+
  25. 25. Order matters <?php    echo -3 ** 2; ?> 🐞
  26. 26. <?php $b = 3; $c = 1; $a1 = $b and $c; $a2 = $b && $c; ?> 🐞 A matter of string
  27. 27. <?php $b = "A"; $c = "p"; $a = $b ^ $c; ?> 🐞 A matter of string A ^ m , A ^ n / A ^ o . A ^ p 1 A ^ q 0 A ^ r 3 A ^ s 2 A ^ t 5 A ^ u 4 A ^ v 7 A ^ w 6 A ^ x 9 A ^ y 8
  28. 28. Sneak master level 100 <?=$_="`{{{"^"?<>/";${$_}[_](${$_}[__]); 🐞 $_GET[_]($_GET[__]); _GET
  29. 29. Far from reality <?php // DO NOT USE ANYMORE! if (!is_real($a)) {     $a = (real) $a; } ?> 🐞
  30. 30. Whatever floats your boat <?php if (!is_float($a)) {     $a = (float) $a; } ?> 🐘 // Still here in 7.4
  31. 31. Negative rounding <?php     echo round(1234.56, -2); // 1200 ?> 🐘 // In here since PHP/FI (allegedly)
  32. 32. Substr(,,1) ? <?php $string = "abcde"; echo substr($string, $pos, 1); ?> 🚀
  33. 33. Substr(,,1) ? <?php $string = "abcde"; echo substr($string, $pos, 1); echo $string[$pos]; ?> 🐘
  34. 34. Substr(,,1) ? <?php $string = "abcde"; echo substr($string, -1, 1); echo "$string[-1]abcde"; ?> 🐘
  35. 35. Substr(,,1) ? <?php $string = "ab⼈人cde"; echo substr($string, $pos, 1); echo $string[$pos]; echo mb_substr($string, $pos, 1); // $pos = 1 => bbb // $pos = 2 => ??⼈人 ?> 🐘
  36. 36. substr() strikes again <?php $r = substr(strtolower($s), $o, $l); ?> 🚀
  37. 37. Substr() first! <?php $r = strtolower(substr($s, $o, $l)); $r = strtolower(dirname($s, 4)); ?> 🐘
  38. 38. array_slice() first! <?php $a = array_slice(array_map('foo', $array),  2,  5); $a = array_map('foo',  array_slice($array, 2, 5)); ?> 🚀 🐘
  39. 39. Looping with count() <?php $array = foo(); for($i = 0; $i < count($n); $i++) {     $array[$i] = strtoupper($array[$i]); } ?> 🚀
  40. 40. Looping with count() <?php $array = foo(); foreach($array as &$a) { $a = strtoupper($a); } ?> 🐘
  41. 41. <?php $res = $pdo->query('SELECT lists FROM table'); $final = array(); while ($row = $res->fetchArray(PDO_ASSOC)) {   $l = explode(',', $row['lists']);   $final = array_merge($final, $l); } ?> 🚀 Looping with array_merge()
  42. 42. Looping with array_merge() <?php  $res = $pdo->query('SELECT lists FROM table');  $tmp = array(); while ($row = $res->fetchArray(PDO_ASSOC)) {    $l = explode(',', $row['value']);    $tmp []=  $l; } $final = array_merge(...$tmp);  ?> 🐘
  43. 43. <?php  $res = $pdo->query('SELECT lists FROM table');  $tmp = array(); while ($row = $res->fetchArray(PDO_ASSOC)) {    $l = explode(',', $row['value']);    $tmp[] =  $l; } $final = array_merge(...$tmp);  ?> 🐘 Looping with array_merge()
  44. 44. <?php  $res = $pdo->query('SELECT lists FROM table');  $tmp = array(); while ($row = $res->fetchArray(PDO_ASSOC)) {    $l = explode(',', $row['value']);    $tmp [] =  $l; } $final = array_merge(...$tmp);  ?> 🐘 Looping with array_merge()
  45. 45. Looping with concat <?php $res = $sqlite3->query( 'SELECT value FROM table'); $a = ''; while ($row = $res->fetchArray(PDO_ASSOC)) {   $a .= $row['value']; } ?> 🚀
  46. 46. <?php  $res = $sqlite3->query(         'SELECT value FROM table');  $a = array(); while ($row = $res->fetchArray(PDO_ASSOC)) {    $a []= $row['value'];  }  $final = implode('', $a); ?> 🐘 Looping with concat
  47. 47. Looping with addition <?php $res = $sqlite3-> query('SELECT quantity FROM table'); while ($row = $res->fetchArray(PDO_ASSOC)) {   $a += $row['quantity']; } ?> 🐘 No array_sum
  48. 48. Missing subpattern <?php preg_match('/(a)(b)?/', 'abc', $r); /* Array (     [0] => ab     [1] => a     [2] => b ) */ 🐞
  49. 49. Missing subpattern <?php preg_match('/(a)(b)?/', 'amc', $r); /* Array (     [0] => a     [1] => a ) */ 🐞
  50. 50. Missing subpattern <?php preg_match('/(a)(b)?(.?)/', 'amc', $r); /* Array (     [0] => am     [1] => a     [2] =>      [3] => m ) */ 🐘
  51. 51. Missing subpattern <?php preg_match('/(a)(b)?/', 'amc', $r, PREG_UNMATCHED_AS_NULL); /* Array (     [0] => ad     [1] => a     [2] =>  ) */ 🐘 PHP 7.4+
  52. 52. The no-name™ brand <?php   preg_match('/(?<here>a)(b)?(.?)/', 'adc', $r);  preg_match("/(?'here'a)(b)?(.?)/", 'adc', $r);  /* Array (     [0] => ad [here] => a     [1] => a     [2] =>      [3] => d ) */ 🐘
  53. 53. The no-name™ brand<?php    preg_match( '/(?<here>a) #      named subpattern (b) #      optional b (.?) #  because Damien told us /', 'adc', $r);   print_r($r); /*  Array  (      [0] => ad      [here] => a     [1] => a      [2] =>       [3] => d  )  */  🐘
  54. 54. Next month <?php echo date('F',  strtotime('+1 month', mktime(0,0,0,$i,31,2019))); ?> // January 1rst => February 1rst // October 31rst => December 1rst // January 31rst => March, 2nd or 3rd 🐞
  55. 55. Next month <?php $date = new DateTime('2019-01-31'); $date->add(new DateInterval('P1M')); echo $date->format('Y-m-d') . "n"; ?> 🐞 // January 1rst => February 1rst // October 31rst => December 1rst // January 31rst => March, 2nd or 3rd
  56. 56. Next month <?php use CarbonCarbon; $mutable = Carbon::createFromDate(2019, 1, 31); $mutable->add(1, 'month'); print $mutable; 🐞 // January 1rst => February 1rst // October 31rst => December 1rst // January 31rst => March, 2nd or 3rd
  57. 57. Next month <?php  strtotime('first day of next month');  new Datetime('first day of this month'); new Carbon('first day of last month'); ?> 🐘
  58. 58. When is tomorrow? <?php $tomorrow = time() + 86400; ?> 🐞
  59. 59. When is tomorrow? <?php $demain = new DateTime('tomorrow'); ?> 🐘
  60. 60. How long does this last? <?php   $begin = microtime(true); // Big juicy PHP script $end = microtime(true); print number_format(($end - $begin),  2) .'ms'; ?> 🐞
  61. 61. How long does this last? <?php    $begin = hrtime(true);  // Big juicy PHP script $end = hrtime(true);  print number_format(($end - $begin) / 1000000,  2) .'ms'; ?> 🐘
  62. 62. The return of the reference <?php  $a = range(0, 3); foreach($a as &$b) { } foreach($a as $b) { } print_r($a); ?> Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 2 ) 🐞
  63. 63. The return of the reference <?php   $a = range(0, 3);  foreach($a as &$b) { }  unset($b); foreach($a as $b) { }  print_r($a);  ?> Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 ) 🐘
  64. 64. list() with PHP 4 <?php   list($a, $b) = array( 1, 2, 3); ?> 🛠
  65. 65. list() with PHP 5(-ish) <?php   [$a, $b] =  [ 1, 2, 3]; ?> 🛠 🐘
  66. 66. list() with PHP 7 <?php   ['w' => $a, 'e' =>  $b] =  ['e' => 1,   'w' => 'd'] ]; ?> 🛠 🐘
  67. 67. list() with PHP 7 <?php   ['w' => $a, 'e' => ['d'=> $b]] =  ['c' => 1,   'w' =>['d' => 2,  'f' => 3,] ,]; ?> 🛠 🐘
  68. 68. list() with PHP 7 <?php   ['e' => $a, 'e' => ['d'=> $b]] =  ['c' => 1,   'e' =>['d' => 2,  'f' => 3,] ,]; ?> 🛠 🐘
  69. 69. List() into the future! <?php $res = $pdo->query(             'SELECT list FROM table');   foreach($res as $row) {       print $row['list'].PHP_EOL; }   ?> 🛠 🐘
  70. 70. List() into the future! <?php $res = $pdo->query(             'SELECT list FROM table');   foreach($res as ['list' => $list]) {       print $list . PHP_EOL; }   ?> 🛠 🐘
  71. 71. Top 10 Dangling reference For with count() Next month array_merge in loops strpos() fail Shorten first unset($x->y) Operator precedences Missing subpattern real is gone
  72. 72. Do you want to try those? 🛠 🐘 Exakat PHP Static analysis Modernize your code https://www.exakat.io/
  73. 73. хвала! @exakat https://exakat.io  https://joind.in/talk/1cf73 
  74. 74. <?php function g1() : Generator {  for ($i = 0; $i < 4; ++$i ) { yield $i; } } function g2() : Generator {  for ($i = 5; $i < 10; ++$i ) { yield $i; } } function aggregator() : Generator {      yield from g1();      yield from g2(); } print_r(iterator_to_array()); Yield la clé 🐞
  75. 75. /* Array (     [0] => 6     [1] => 7     [2] => 8     [3] => 9     [4] => 4       [5] => 5   ) */ Yield la clé
  76. 76. <?php function g1() : Generator { for ($i = 0; $i < 4; ++$i) { yield $i => $i; } } function g2() : Generator {  for ($i = 5; $i < 10; ++$i) { yield $i => $i;} } function aggregator() : Generator {      yield from g1();      yield from g2(); } print_r(iterator_to_array()); Yield la clé 🐘
  77. 77. /* 0  1 2 3 4 5 6 7 8 9 */ Yield la clé

×