fork(1) download
  1. <?php
  2. /**
  3.  * The difference (or striking similarity) between using `strpos` or `strpbrk` in PHP
  4.  *
  5.  * Note: The variable naming scheme used in this code is an adaption of
  6.  * Systems Hungarian which is explained at http://p...content-available-to-author-only...r.ca/VariableNamingConvention/
  7.  */
  8.  
  9.  
  10. function checkStrPos ($p_sFormat)
  11. {
  12. return strpos($p_sFormat, 'H') === false
  13. && strpos($p_sFormat, 'i') === false
  14. && strpos($p_sFormat, 's') === false
  15. ;
  16. }
  17.  
  18. function checkStrPbrk($p_sFormat)
  19. {
  20. return strpbrk($p_sFormat, 'His') === false;
  21. }
  22.  
  23.  
  24. $aFormats = array(
  25. '' => true
  26. , 'H:i:s' => false
  27. , 'His' => false
  28. , 'siH' => false
  29. , 'H' => false
  30. , 'i' => false
  31. , 's' => false
  32. , 'h' => true
  33. , 'I' => true
  34. , 'S' => true
  35. , 'A:B:C' => true
  36. , 'A:B:C H:i:s' => false
  37. , 'A:B:C H' => false
  38. , 'A:B:C i' => false
  39. , 'A:B:C s' => false
  40. );
  41.  
  42. foreach ($aFormats as $t_sFormat => $t_bExpectedValue) {
  43. echo var_export(
  44. checkStrPbrk($t_sFormat) === $t_bExpectedValue
  45. && checkStrpos($t_sFormat) === $t_bExpectedValue
  46. && checkStrPbrk($t_sFormat) === checkStrpos($t_sFormat)
  47. , true
  48. ) . PHP_EOL
  49. ;
  50. }
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true