note 105614 added to function.strstr
| From: | xydon1 at yahoo dot com | Date: | Wed, 31 Aug 2011 22:03:41 +0000 |
| Subject: | note 105614 added to function.strstr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182871@lists.php.net to get a copy of this message | ||
I explained this to my newbies the difference though it would be good to demonstrate this here. If
you are evaluating a list you cannot use strstr as it looks for any occurance. The correct way is
the function which evaluates a true or false on the exact value to check.
In the following example I have a list of promotion ids I want to evaluate whether or not a
promotion exists:
//Wrong
$promotion = strstr("25,56","2");
if($promotion){
echo "found";
}else{
echo "not found";
}
//right
function detectPromotion($string,$promotion){
foreach(explode(",",$string) as $promotionToCheck){
echo "checkin ".$promotionToCheck."==".$promotion."<br/>";
if($promotionToCheck==$promotion){
$promotionFound=true;
}
}
return $promotionFound;
}
echo detectPromotion("2,56","2");
----
Server IP: 69.147.83.197
Probable Submitter: 173.14.9.174
----
Manual Page -- http://www.php.net/manual/en/function.strstr.php
Edit -- https://master.php.net/note/edit/105614
Del: integrated -- https://master.php.net/note/delete/105614/integrated
Del: useless -- https://master.php.net/note/delete/105614/useless
Del: bad code -- https://master.php.net/note/delete/105614/bad+code
Del: spam -- https://master.php.net/note/delete/105614/spam
Del: non-english -- https://master.php.net/note/delete/105614/non-english
Del: in docs -- https://master.php.net/note/delete/105614/in+docs
Del: other reasons-- https://master.php.net/note/delete/105614
Reject -- https://master.php.net/note/reject/105614
Search -- https://master.php.net/manage/user-notes.php