1

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

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Jaume Mal
  • 546
  • 5
  • 21

2 Answers2

2

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

strpos returns false when the $needle is not found in the $html. You can learn more about the return value of strpos here http://php.net/manual/en/function.strpos.php

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140