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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.