Search code examples
phpbooleanvariable-assignmentassignment-operator

In PHP when would a variable assignment return false?


When would a PHP variable assignment return false?

In this answer the following code is suggested

while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
    $positions[] = $lastPos;
    $lastPos = $lastPos + strlen($needle);
}

... the while loop will end when the assignment...

$lastPos = strpos($html, $needle, $lastPos)

...returns false.

When would this assignment return false and why?

Thanks


Solution

  • A variable assignment returns the value you assigned to the variable. So when the strpos call returns false (when the $needle isn't found), so will the assignment, and the loop will terminate.