Taylor Reed
Taylor Reed

Reputation: 355

if else with $_GET not working

What I am trying to do is echo out based on the $_GET['page'] variable however its echoing this out for all of my navigation menu's. How can I only have it use the page I am on? This is only the php part of it, the actual html code goes below each line however for simplicity I did not include it

<?php if($_GET['page'] == 'user_edit' || 'NULL'){ echo "<li class=\"current\">"; } else{ echo "<li>";} ?>
<?php if($_GET['page'] == 'manage' || 'add' || 'ftp'){ echo "<li class=\"current\">"; } else{ echo "<li>";} ?>
<?php if($_GET['page'] == 'bugs' || 'support' || 'notifications'){ echo "<li class=\"current\">"; } else{ echo "<li>";} ?>

Upvotes: 0

Views: 177

Answers (2)

deceze
deceze

Reputation: 522015

It does not work like this. Your conditions are evaluated as:

if ( ( $var == 'foo' ) || ( 'bar' ) || ( 'baz' ) )

Note the grouping above. All three are separate expressions. It does not mean "if $var equals 'foo', 'bar' or 'baz'", it means "if $var equals 'foo' is true or 'bar' is true or 'baz' is true".

And 'bar' is always true.

You need:

if ($var == 'foo' || $var == 'bar' || $var == 'baz')

or

if (in_array($var, array('foo', 'bar', 'baz'))

Upvotes: 1

Alex Howansky
Alex Howansky

Reputation: 53533

This is no good:

if ($_GET['page'] == 'manage' || 'add')

In this case, the string 'add' evaluates as boolean true, which means the condition will fire every time. You want this instead:

if ($_GET['page'] == 'manage' || $_GET['page'] == 'add')

Or this:

if (in_array($_GET['page'], array('manage', 'add')))

Upvotes: 2

Related Questions