Forum Moderators: coopster
if (stristr('haystack', 'needle')
This returns TRUE if 'needle' is found in the haystack string.
Q1)
How would I go about writing up this pseudocode
if (stristr('haystack', 'needle1' or 'needle2' or 'needle3')
That is - I want it to return TRUE if either 'needle1', 'needle2' or 'needle3' are
found in the haystack string. If none of them are found - it returns FALSE.
IS there any better way than:
if ((stristr($haystack, 'needle1') or (stristr($haystack, 'needle2') or (stristr($haystack, 'needle3'))
I am especially worried because I may well be searching the same haystack for a lot of needles. And the above approach seems a bit messy to me for a lot of needles. There must be a more elegant way.
Q2)
Consider this code. Note the! - the!stristr
if (stristr('haystack', 'needle') &&!stristr('haystack', 'needle2')
Is there anyway to make this code smaller? - faster? - nicer? Given that both elements
are dealing with the same haystack.
Q3)
I take it that these two lines are equivalent
if ($variable!== 23 or 35 or 67 or 12)
if ($variable!== 23 ¦ 35 ¦ 67 ¦ 12)
Is there any nicer, cleaner, faster way of doing this. Could I perhaps put all the
elements in an array - and then somehow use this array against the $variable element. WOuld
this approach be at all faster? How would I go about this? There are not many numbers in the lines above - but in my actual code I could actually be using a lot of numbers.
I realise that this stuff is pretty basic. But would be v.grateful for any help.
if (stristr('haystack', 'needle1' or 'needle2' or 'needle3')
That is - I want it to return TRUE if either 'needle1', 'needle2' or 'needle3' are
found in the haystack string. If none of them are found - it returns FALSE.
IS there any better way than:
if ((stristr($haystack, 'needle1') or (stristr($haystack, 'needle2') or (stristr($haystack, 'needle3'))
Because if I use this method - with the number of needles I will be using - will be very long and messy. Is there anyway that I can do this "cleaner".
If anyone cant figure out what I mean - please do post and I will try to clarify more. I may not have explained the isse very clearly.
$array_needles = array("needle1", "needle2", etc...);
$array_found_needles = array();$haystack = "haystack";
foreach($array as $key=>$val){
if(stristr($haystack, $val){
//do whatever you want if its found
echo "$val was found"
}
}
ON a second query:
if ($variable!== 0 ¦ -60 ¦ -120 ¦ 480 ¦ 420 ¦ 360 ¦ 300 ¦ 240 ¦ 180)
so if the $variable does NOT have the value 0 or -60 or -120...... return true.
BUT I cannot get the above line to work. What is wrong with it?
As far as this post above goes, you cannot just specify each item, you have to explicitely redine the expression for each comparison number.
So this statement:
if ($variable!== 0 ¦ -60 ¦ -120 ¦ 480 ¦ 420 ¦ 360 ¦ 300 ¦ 240 ¦ 180)
Is saying: If $variable is NOT equal in both value and type to the integer 0 OR -60 evaluates to true, OR -120 evaluates to true, OR... etc etc..
When it should be:
if ($variable!== 0 ¦ $variable!== -60 ¦ $variable!== -120 ¦ $variable!== 480 ¦ $variable!== 420 ¦ $variable!== 360 ¦ $variable!== 300 ¦ $variable!== 240 ¦ $variable!== 180)
Which says: If $variable is NOT equal in both value and type to the integer 0 OR $variable is NOT equal in both value and type to the integer -60 OR $variable is NOT equal in both value and type to the integer -120 OR etc, etc....
See? Every comparison has to be defined by the complete expression seperately. For a situation like this, you'd use a switch statement:
switch ($variable) {
case 0:
//do something
break;
case -60:
//do something
break;
case -120:
//do something
break;
case 480:
//do something
break;
case 420:
//do something
break;
case 360:
//do something
break;
case 300:
//do something
break;
case 240:
//do something
break;
case 180:
//do something
break;
default:
// Do some default method..
break;
}