Forum Moderators: coopster

Message Too Old, No Replies

IF checking on mutiple values of a single variable

simplest and fastest way

         

smallcompany

2:52 pm on Aug 17, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For outgoing link maintenance in PHP, I use:

if ($var == "link1") {$link = "http://www.example.com/";}

Now, there are different values for the same variable $var, using the same outgoing link. So, link1 an link2 may point to the same site.

Therefore, I learned that instead of having the two in two lines, I could do this:

if ($var == "link1" || $var == "link2") {$link = "http://www.example.com/";}

Then I wondered why I simply could not do if ($var == "link1 || link2") {$link = "http://www.example.com/";}

and could not find a definite answer (my guess is that all under one pair of quotes counts as one value),

but then came across examples with arrays:

if ( !in_array($var, array('link1','link2'), true ) ) {$link = "http://www.example.com/";}

Now I wonder, since in a single PHP redirect file, I may have 10 or 20 different outgoing links, what would be simplest and fastest way for redirect values that use identical outgoing links - simple OR double pipe option or arrays (it would be multiple in a single file)

Thanks

cffrost2

12:03 am on Aug 18, 2016 (gmt 0)

10+ Year Member



On the fly.... I'd do this. May be a better way...


Function link($link) {
$link1 = array('thisLink1' ,'thatLink1' ,'theOtherLink1' , 'anotherLink1', 'lastLink1');

$link2 = array('thisLink2' ,'thatLink2' ,'theOtherLink2' , 'anotherLink2', 'lastLink2');

if(in_array($link, $link1) {
$newLink = 'http://www.whereiwanttogo1.com';
}
elseif(in_array($link, $link2)) {
$newLink = 'http://www.whereiwanttogo2.com';
}
else {
$newLink = ''; //default link here if it's not found at all
}

return $newLink;
}


Or alternatively...
 
function link($link) {
$link1 = 'http://www.link1.com';
$link2 = 'http://www.link2.com';
$link3 = 'http://www.link3.com';
$link4 = 'http://www.link4.com';
$link4 = 'http://www.link4.com';

$linkList = array(
'linkKey1' => $link1,
'linkKey2' => $link1,
'linkKey3' => $link4,
'linkKey4' => $link2,
'linkKey5' => $link3,
'linkKey6' => $link3,
'linkKey7' => $link1,
'linkKey8' => $link2,
'linkKey9' => $link2,
'linkKey10' => $link4
);
If(in_array($link, $linkList, true) {
return $linkList[$link];
}
else {
return 'Link not found'; //or some other not found handler
}
}


Off the top of my head.
Not tested. Just quickly written. Hope this helps.

whitespace

2:59 pm on Aug 19, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



tl;dr Use arrays, as in cffrost2's post. (see my comment below)


Then I wondered why I simply could not do if ($var == "link1 || link2") {$link = "http://www.example.com/";}

and could not find a definite answer (my guess is that all under one pair of quotes counts as one value),


Yes, everything inside the quotes is a single string value. I suppose it might appear "more correct" to have something like: if ($var == "link1" || "link2") ... however, that is invalid PHP syntax.

What you could do is employ a PHP function call... for example, since we are dealing with string comparisons, the strstr() function [php.net] returns the string that is found, or FALSE if it's not found. So, the above could be written as:


if (strstr('link1,link2',$var)) {$link = "http://www.example.com/";}


The value of $var is searched for in the string "link1,link2" and will return the portion of the string (to the end) that contains that value. If it's not found then (bool)FALSE is returned. The important thing here is that a non-empty string evaluates to TRUE.

However, strstr() is not particularly efficient, so if performance is key (I don't think it is here), then the marginally more verbose strpos() [php.net] would be preferable:


if (strpos('link1,link2',$var) != false) {$link = "http://www.example.com/";}


Checking against (bool)false is required if $var could match at the start of the string (ie. position 0, which would evaluate to false, but is a positive match). If you can be sure that $var will never match at the start of the string, then it can be simplified. For example:


if (strpos('@link1,link2',$var)) {$link = "http://www.example.com/";}



if ( !in_array($var, array('link1','link2'), true ) ) {$link = "http://www.example.com/";}


If this is intended to be equivalent to your earlier examples, then you should probably remove the "!" prefix on the expression, since this negates the whole expression... only if $var does not match. Providing $var is always a string, you probably don't need the strict comparison (3rd param - true).

However, my preference would be to use arrays to store your links and compare against that array - very much like cffrost2's "Or alternatively..." suggestion above. It's easier to read and maintain (and debug), more flexible and could well be shorter code wise.

cffrost2

12:17 am on Aug 28, 2016 (gmt 0)

10+ Year Member



Excellent detailed explanation! Thanks!