Page 1 of 1

Conditional statment in the middle of output?

Posted: Fri Apr 04, 2003 11:50 am
by MattSharp
Is it somehow possible to have an if statement in the middle of an echo. Like:

Code: Select all

echo "bill likes" . if ($likes =="yes") echo "me" . "!!!!!!"
I tried code similar to that without any luck. I wondered if there was a way to get around it or make it work. Cause there is a certain part in the code where I want to output something if an if statement is true and nothing if its false. Thanks.

Posted: Fri Apr 04, 2003 12:08 pm
by hedge
echo "bill likes" . (($likes=='yes') ? 'me' : '!!!!!!')

Posted: Fri Apr 04, 2003 12:26 pm
by Jim
You could also try a solution like this:

Code: Select all

<?
if($likes == 'yes') {
$answer = "me!";
} else {
$answer = '';
}

echo "Bill likes $answer";

?>

Posted: Fri Apr 04, 2003 12:44 pm
by twigletmac
You could simplify:

Code: Select all

if($likes == 'yes') { 
$answer = "me!"; 
} else { 
$answer = ''; 
}
to a one-liner using the ternary operator like hedge had in his code:

Code: Select all

$answer = ($likes == 'yes') ? 'me!' : '';
Mac