Option 1:

if ( $a == $b && $a == $c && $a == $e ) 

Option 2:

 if ( preg_match("/^$a/", $b) && preg_match("/^$a/", $c) && preg_match("/^$a/", $e) ) 

Which one to choose and why?

PS I assume that the first option is performed faster, but still, I want to hear the opinions of users who really know.

    3 answers 3

    preg_match is unlikely to be faster than the usual comparison, since it needs at least to load the parser controller. A comparison will cause itself strcmp and all business. But what happens if $ a is a regular expression ...

      In the first variant, equality is checked, and in the second, the occurrence. 1) 1 = 1fghfgh: true 2) 1sdfsdf3 = 1: false, because not included in the substring

       $a = "1ertert"; $b = 1; if ($a == $b ) echo "true<br/>"; else echo "false<br/>"; if (preg_match("/^$a/", $b)) echo "true<br/>"; else echo "false<br/>"; 
      • Those. the variant with equalities is only suitable for numeric expressions, and for plain text? For example, for html page code? - nick777
      • <pre> <code> <? php $ a = "a"; $ b = "a"; if ($ a == $ b) echo "true <br/>"; else echo "false <br/>"; ?> </ code> </ pre> <pre> <code> <? php $ a = "a"; $ b = "b"; if ($ a == $ b) echo "true <br/>"; else echo "false <br/>"; ?> </ code> </ pre> What prevents to check? - or_die

      I would advise this:

       if ( $a === $b === $c === $e ) 

      === is a strict comparison, without type conversion.