View Full Version : Simple if () conditional using strings

11-19-2010, 03:15 AM
Hey guys,

Kinda new to this php lark and wasn't able to find anywhere that could explain why this is not working correctly -

Basically i'm working on a menu that changes the UL background according to which section the page is currently on.

I have a template index.php page that changes according to index.php?page=pagename

What I have in the menu is -

$pagename = $_GET['page'];
function setcurrent($name) {
echo "<ul class='section ".$name." current'>";
if ($pagename == 'whybamboo' || 'environmentallysound' || 'ourclients') {
echo "<ul class='section whybamboo'>";

Can anyone explain to me why the || operators are not working and if there is a simpler way of achieving this.

I also have a problem where i'd like the title of the page to change according to the ?page=pagename variable also. Now while I can appreciate that this could be done with an array or switch conditional, what is the most common way that dynamic titles on web pages is achieved?


11-19-2010, 03:40 AM
if ($pagename=='whybamboo' || $pagename=='environmentallysound' || $pagename=='ourclients') {
setcurrent($pagename); //or setcurrent('whybamboo');//see below

I am not sure if the above is actually doing what you want it to do , are you saying IF whybamboo OR environmentally sound OR ourclients then regardless send to 'whybamboo' , or do you want to setname() whatever the $_GET['page'] is ?

if so an array of allowable names might be a plan..

$okpages = array('whybamboo','evironmentallysound','ourclients','blah');
$htmltitle = $_GET['page'];
echo 'invalid page selection';

<html title="<?=$htmltitle;?>">

hope I am on the right track here ?

11-19-2010, 04:06 AM
First solution worked great! Simple conditional error.

I didn't think there was any difference between them but yea:

if ($variable == 'a' || 'b' || 'c')



if ($variable == 'a' || $variable == 'b' || $variable == 'c')