Jaume Mal
Jaume Mal

Reputation: 658

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

Upvotes: 1

Views: 655

Answers (2)

user1377396
user1377396

Reputation:

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

Upvotes: 1

Mureinik
Mureinik

Reputation: 311998

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.

Upvotes: 2

Related Questions