Jump to content


Photo

Ternary Operator


  • Please log in to reply
5 replies to this topic

#1 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 19 June 2006 - 08:51 PM

Is it possible to use a ternary operator on the same line as an echo (using the conditional to modify the echo)?
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 19 June 2006 - 09:02 PM

Yes; try it out.

<?php
    echo 0 ? 'true' : 'false';
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 19 June 2006 - 09:06 PM

My fault for not making myself clear. What I mean is is it possible to use a ternary operator in the middle of an echo?

Something that would mimic:
echo 'You have';
if($a >= 4){
   echo 'equal to or more than 4';
}else{
   echo 'less than 4';
}
echo "items<br />\n";

I can't find a way to do that with ternary operator (without including the whole phrase, You...items, on each evaulation of the conditional).
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#4 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 19 June 2006 - 09:11 PM

You might have to use parantheses to make it unambiguous, but, yes:
echo "You have " . ($a < 4 ? 'less than four' : 'four or more') . " items.";

Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#5 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 19 June 2006 - 09:22 PM

[!--quoteo(post=385797:date=Jun 19 2006, 05:11 PM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 19 2006, 05:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You might have to use parantheses to make it unambiguous, but, yes:
echo "You have " . ($a < 4 ? 'less than four' : 'four or more') . " items.";
[/quote]
It's not working for me.

echo '<h5 class="class">' . $row['a'] . ((isset($row['b'])) ? 'text') . "</h5><br />\n"; //line 30
is producing
Parse error: syntax error, unexpected ')' in page.html on line 30

Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#6 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 19 June 2006 - 09:26 PM

[!--quoteo(post=385800:date=Jun 19 2006, 05:22 PM:name=JohnnyK)--][div class=\'quotetop\']QUOTE(JohnnyK @ Jun 19 2006, 05:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It's not working for me.

echo '<h5 class="class">' . $row['a'] . ((isset($row['b'])) ? 'text') . "</h5><br />\n"; //line 30
is producing
Parse error: syntax error, unexpected ')' in page.html on line 30
[/quote]

That's because your code snippet is missing the rest of the ternary operator (the colon part); if you don't need a false condition, at least put an empty string:
... . (isset($row['b']) ? 'text' : '') . ...
PHP is expecting that, instead it's running into an "unexpected parenthesis."
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users