IF statement inside echo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

IF statement inside echo

Post by phpnoobyyy »

Hi guys

As my name suggests, i'm new to php so please go easy on me.

I'm trying to output (unsuccessfully thus far) the following inside an echo:

Code: Select all

 
echo '
<li><a href="prices.php" class="topnav1">Prices</a></li>
<li><a href="order.php" class="prinav">Place an order</a></li>
<li class="linav"><?php if ( (isset($_SESSION[\'user_id\'])) && (!strpos($_SERVER[\'PHP_SELF\'], \'sign_out.php\')) ) {
echo \'<a href="sign_out.php">Sign out</a>\';}
else {
echo \'<a href="sign_in.php">Sign in</a>\';
}       
?></li>';
 
Here's the same php code outside the

Code: Select all

bubble:
 
echo '
<li><a href="prices.php" class="topnav1">Prices</a></li>
<li><a href="order.php" class="prinav">Place an order</a></li>
<li class="linav"><?php if ( (isset($_SESSION[\'user_id\'])) && (!strpos($_SERVER[\'PHP_SELF\'], \'sign_out.php\')) ) {
echo \'<a href="sign_out.php">Sign out</a>\';}
else {
echo \'<a href="sign_in.php">Sign in</a>\';
}       
?></li>';
 
I've googled for solutions but nothing seems to work. Help greatly appreciated....thanks
 
PHPnoobyyy
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: IF statement inside echo

Post by superdezign »

An echo statement must be in <?php ?> tags. You can't have <?php ?> tags inside of <?php ?> tags.

Code: Select all

echo '<li><a href="prices.php" class="topnav1">Prices</a></li>'
   . '<li><a href="order.php" class="prinav">Place an order</a></li>'
   . '<li class="linav">';
 
if ( (isset($_SESSION['user_id'])) && (!strpos($_SERVER['PHP_SELF'], 'sign_out.php')) ) {
    echo '<a href="sign_out.php">Sign out</a>';
} else {
    echo '<a href="sign_in.php">Sign in</a>';
}
 
echo '</li>';
Last edited by superdezign on Mon Oct 12, 2009 4:03 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IF statement inside echo

Post by John Cartwright »

You cannot embed php inside a string, you will either need to break out of php or build the string sequentially.

Code: Select all

$output = '
   <li><a href="prices.php" class="topnav1">Prices</a></li>
   <li><a href="order.php" class="prinav">Place an order</a></li>
   <li class="linav">
';
 
if ( (isset($_SESSION['user_id'])) && (!strpos($_SERVER['PHP_SELF'], 'sign_out.php')) ) {
   $output .= '<a href="sign_out.php">Sign out</a>';}
else {
   $output .= '<a href="sign_in.php">Sign in</a>';
}      
$output .= '</li>';
 
echo $output;
Last edited by John Cartwright on Mon Oct 12, 2009 4:03 pm, edited 1 time in total.
Reason: Too late :(
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: IF statement inside echo

Post by phpnoobyyy »

That's great, it works.

Thanks a lot, really appreciated.
Post Reply