tags:

views:

15

answers:

3

Hi!

This is the situation: I have a list of items, and one of LI is:

echo '<li><a href= "index.php" title="">Link</a></li>';

Now, I want to make an if statement here - if user is logged in, give home.php, else give index.php - but I'm kinda lost in all those "s and 's and .s so I'm asking for your help

This code won't do :/

echo '<li><a href= "' . if($logged == 0) echo "index.php" else "home.php" . '" title="">Link</a></li>';

Also, I know I could do it with this code, but I want to finally get those dots and stuff

if ($logged == 0) 
{
    echo '<li><a href= "index.php" title="">Link</a></li>';
}
else
{
    echo '<li><a href= "home.php" title="">Link</a></li>';
}
A: 
<?php echo '<li><a href="' . ($logged == 0) ? 'index.php' : 'home.php' . '" title="">Link</a></li>'; ?>
aadravid
A: 

If you are after a short and concise solution try this:

<li><a href="<?php echo $logged ? 'home.php' : 'index.php'; ?>" title="">Link</a></li>

This is called a ternary operator. If $logged evaluates to true it will print 'home.php', otherwise it will print 'index.php'.

Here's an equivalent in standard if-else notation:

<li><a href="<?php if($logged){ echo 'home.php';} else{ echo 'index.php';} ?>" title="">Link</a></li>
silvo
Thank you guys!Lifesavers ;)
Nik
A: 

You cannot use if's and echo's in echo parameters.

echo '<li><a href= "'. $logged == 0 ? "index.php" : "home.php" .'" title="">Link</a></li>';
mhitza