I know this is simple PHP logic but it just won\'t work...
$str = \"dan\";
if(($str != \"joe\")
|| ($str != \"danielle\")
|| ($str != \"heather\")
Welcome to boolean logic:
$str = 'dan'
$str != "joe" -> TRUE, dan is not joe
$str != "danielle" -> TRUE, danielle is not dan
$str != "heather") -> TRUE, heather is not dan
$str != "laurie" -> TRUE, laurie is not dan
$str != "dan" -> FALSE, dan is dan
Boolean logic truth tables look like this:
and:
TRUE && TRUE -> TRUE
TRUE && FALSE -> FALSE
FALSE && FALSE -> FALSE
FALSE && TRUE -> FALSE
or:
TRUE || TRUE -> TRUE
TRUE || FALSE -> TRUE
FALSE || TRUE -> TRUE
FALSE || FALSE -> FALSE
Your statement boiled down to:
TRUE || TRUE || TRUE || TRUE || FALSE -> TRUE
try this
$str = "dan";
if($str == "joe" || $str == "daniella" || $str == "heather" || $str == "laurine" || $str == "dan"){ ... }
When you compare two strings you have to use strcmp().
I am not exactly sure what you want, but that logic will always evaluate to true
. You might want to use AND (&&), instead of OR (||)
The furthest statement that is ever tested is ($str != "danielle"
) and there are only two possible outcomes as PHP enters the block as soon as a statement yields true.
This is the first:
$str = "dan";
$str != "joe" # true - enter block
$str != "danielle" #ignored
$str != "heather" #ignored
$str != "laurie" #ignored
$str != "dan" #ignored
This is the second:
$str = "joe";
$str != "joe" # false - continue evaluating
$str != "danielle" # true - enter block
$str != "heather" #ignored
$str != "laurie" #ignored
$str != "dan" #ignored
If the OR was changed to AND then it keeps evaluating until a false is returned:
$str = "dan";
$str != "joe" # true - keep evaluating
$str != "danielle" # true - keep evaluating
$str != "heather" # true - keep evaluating
$str != "laurie" # true - keep evaluating
$str != "dan" # false - do not enter block
The solution doesn't scale well though, you should keep an array of the exclude list and check against that do:
$str = "dan";
$exclude_list = array("joe","danielle","heather","laurie","dan")
if(!in_array($str, $exclude_list)){
echo " <a href='/about/".$str.".php'>Get to know ".get_the_author_meta('first_name')." →</a>";
}
Another approach is
$name = 'dan';
$names = array('joe', 'danielle', 'heather', 'laurie', 'dan');
if(in_array($name,$names)){
//the magic
}
Based on your comment in Glazer's answer, it looks like you want to enter the if block when $str
is not one of the listed names.
In that case it would be more readable if you write it as
if( !( ($str == "joe") || ($str == "danielle") || ($str == "heather") || ($str == "laurie") || ($str == "dan") ) )
This actually reads as "if it's not one of these people..." to someone looking at your code. Which is equivalent to the slightly less obvious
if( ($str != "joe") && ($str != "danielle") && ($str != "heather") && ($str != "laurie") && ($str != "dan") )
The fact that they're equivalent is called DeMorgan's law in logic.