/    Sign up×
Community /Pin to ProfileBookmark

PHP || (and) operator question

I am trying to make my navigation to change color when selected. the class for selected is “current”

so I wrote this:

<ul class=”<?php if(myfullpath() == “http://www.domain.com/first.php” || “http://www.domain.com/second.php”){echo “current”;} else echo ” “; ?>”>

so if the url in the address bar is not first.php or second.php it should return false and echo ” “, but for some odd reason this is returning true and posting class=”current”> even if the url is posting [url]http://www.domain.com/fourth.php[/url].

JUST FYI here is the myfullpath function:

function myfullpath() { $s = empty($_SERVER[“HTTPS”]) ? ” : ($_SERVER[“HTTPS”] == “on”) ? “s” : “”; $protocol = strleft(strtolower($_SERVER[“SERVER_PROTOCOL”]), “/”).$s; $port = ($_SERVER[“SERVER_PORT”] == “80”) ? “” : (“:”.$_SERVER[“SERVER_PORT”]); return $protocol.”://”.$_SERVER[‘SERVER_NAME’].$port.$_SERVER[‘REQUEST_URI’]; } function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }

thanks for your help.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@adarshakbJan 19.2011 — Hi you missed a point there
<ul class="<?php if(myfullpath() == "http://www.domain.com/first.php" || "http://www.domain.com/second.php"){echo "current";} else echo " "; ?>">[/QUOTE]

check If statement

It should be
<ul class="<?php if(myfullpath() == "http://www.domain.com/first.php" || myfullpath() == "http://www.domain.com/second.php"){echo "current";} else echo " "; ?>">[/QUOTE]

PS: I didnt execute this code... have to check yourself
Copy linkTweet thisAlerts:
@andy106authorJan 19.2011 — Hi Thank you that's right I forgot about that, I atually ended up using elseif instead, so :

if(myfullpath() == "http://www.domain.com/first.php"){echo "current";} elseif(myfullpath() == "http://www.domain.com/second.php") {

echo "current";}else echo " ";

I wonder which is a better practice or if there is any difference in server resources.

Thanks again for all your help
Copy linkTweet thisAlerts:
@adarshakbJan 19.2011 — Both, in this case, are the same. As in using A || B it checks A and if true it ignores B. Similarly in ifelse if first IF statement is true it ignores the second.

But from a view of code maintenance its a single if statement thats better.
Copy linkTweet thisAlerts:
@andy106authorJan 19.2011 — yea you are right the elseif statements can become hard to figure out after a while and also || is much shorter and easier to troubleshoot. I have changed it all over to || and it works great! thank you so much !
Copy linkTweet thisAlerts:
@OctoberWindJan 20.2011 — [code=php]
<?php
$fullPath = myfullpath();
$class="";
if (
($fullPath == "http://www.domain.com/first.php") ||
($fullPath == "http://www.domain.com/second.php")
)
{
$class = 'class="current" ';
}
?>

<ul <?php echo $class; ?> >

[/code]



Just a couple of points to add:

-- single call to the function (small improvement to performance, plus a bit easier to maintain/reuse later on)

-- [expanding from adarshakb] wrap [I]each[/I] IF check in it's own parenthesis, then wrap [I]all[/I] the IF checks in parenthesis

-- I prefer to to create the empty $class string, then populate it if needed, and echo out the entire string. This way, you don't leave unnecessary empty 'class=""' in your HTML (again, just a small personal preference)
Copy linkTweet thisAlerts:
@DasherJan 21.2011 — Just for clarification the || operator is the OR operator, not the AND operator which is &&. The thread title is a little confusing in that regard.
Copy linkTweet thisAlerts:
@adarshakbJan 21.2011 — YA!!! didnt notice that ?
×

Success!

Help @andy106 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...