PHP: strpos: zero or false?

Self-tutoring about PHP comparisons: the tutor begins…

In PHP, == means “compare for equality” whereas === means “compare for same type and equality.” Sometimes the difference is important.

strpos(longstring, shortstring) tells where, in the longstring, you will find the shortstring. If the shortstring isn’t in the longstring, false is returned. Yet, if the longstring and shortstring are identical, 0 is returned, since they match starting from 0 (strings start at position 0, not 1).

In PHP 0==false is true, but 0===false isn’t. Therefore, with strpos, === can distinguish between an exact match and no match, but == can’t.

Source:

php.net

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply