My mistake with the strpos function? Php

I'm trying to get a strposstring variable $stringfor the phrase β€œtest” to search , and if it does not contain β€œtest”, another variable is $changeredefined as $string2(where $changeit was defined earlier)

if (strpos($string, 'test') == false) {
  $change = $string2;
  break;
 }      

But, unfortunately, this does not work.

Is there a mistake in the above?

Thanks.

+3
source share
3 answers

strpos returns false if it does not find a string that is equivalent to 0 in a nonspecific conditional expression in PHP. Be sure to use the === operator when comparing strpos usage.

if (strpos($string, 'test') === false) {
  $change = $string2;
  break;
} 
+6
source

Try using

if (strpos($string, 'test') === false)

=== instead of ==
+3
source

strpos "false" "0", false, == ( "" ) === ( ). :

  $change = (strpos($string, 'test') === false) ? $string2 : $change;
+3
source

Source: https://habr.com/ru/post/1720525/


All Articles