If $needle is the last character in $haystack, and testing $needle as a boolean by itself would evaluate to false, then testing strstr() as a boolean will evaluate to false (because, if successful, strstr() returns the first occurrence of $needle along with the rest of $haystack).

<?phpfindZero('01234'); // found a zerofindZero('43210'); // did not find a zerofindZero('0'); // did not find a zerofindZero('00'); // found a zerofindZero('000'); // found a zerofindZero('10'); // did not find a zerofindZero('100'); // found a zero

Also, strstr() is far more memory-intensive than strpos(), especially with longer strings as your $haystack, so if you are not interested in the substring that strstr() returns, you shouldn't be using it anyway.

There is no PHP function just to check only _if_ $needle occurs in $haystack; strpos() tells you if it _doesn't_ by returning false, but, if it does occur, it tells you _where_ it occurs as an integer, which is 0 (zero) if $needle is the first part of $haystack, which is why testing if (strpos($needle, $haystack)===false) is the only way to know for sure if $needle is not part of $haystack.

My advice is to start loving type checking immediately, and to familiarize yourself with the return value of the functions you are using.

If you want to emulate strstr's new before_needle parameter pre 5.3 strtok is faster than using strpos to find the needle and cutting with substr. The amount of difference varies with string size but strtok is always faster.

Often you will need to find all occurrences of a string (for security escapes and such)

So I wrote this function to return an array with the locations of all the occurrences. Almost like an advanced strstr.

<?php
function findall($needle, $haystack)
{
//Setting up
$buffer=''; //We will use a 'frameshift' buffer for this search
$pos=0; //Pointer
$end = strlen($haystack); //The end of the string
$getchar=''; //The next character in the string
$needlelen=strlen($needle); //The length of the needle to find (speeds up searching)
$found = array(); //The array we will store results in

while($pos<$end)//Scan file
{
$getchar = substr($haystack,$pos,1); //Grab next character from pointer
if($getchar!="\n" || buffer<$needlelen) //If we fetched a line break, or the buffer is still smaller than the needle, ignore and grab next character
{
$buffer = $buffer . $getchar; //Build frameshift buffer
if(strlen($buffer)>$needlelen) //If the buffer is longer than the needle
{
$buffer = substr($buffer,-$needlelen);//Truncunate backwards to needle length (backwards so that the frame 'moves')
}
if($buffer==$needle) //If the buffer matches the needle
{
$found[]=$pos-$needlelen+1; //Add the location of the needle to the array. Adding one fixes the offset.
}
}
$pos++; //Increment the pointer
}
if(array_key_exists(0,$found)) //Check for an empty array
{
return $found; //Return the array of located positions
}
else
{
return false; //Or if no instances were found return false
}
}
?>

Haven't had the chance to speed test it, but many optimizations should be possible. It just works enough for me. Hope it saves someone a lot of time.

Please note that $needle is included in the return string, as shown in the example above. This ist not always desired behavior, _especially_ in the mentioned example. Use this if you want everything AFTER $needle.