nikefido Posted February 5, 2008 Share Posted February 5, 2008 can someone really quickly explain the use of "?" and ":" in a conditional statement? ie. rather than using IF ELSE etc you do something like $title ? dothis : orDoThis; //not sure if this is right is this like typing: if($title) { doThis; }else{ orDoThis; } ?? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 yes...the first part is your conditional statement, then after the ? is if it's true and after the : is if it's false. It's use most to shorten up this case: <?php if($test > 3){ $result = 'first case'; }else{ $result = 'second case'; } ?> shortened to <?php $result = $test > 3 ? 'first case' : 'second case'; ?> Quote Link to comment Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 <test_expression>?<true_expression>:<false_expression> U use boolean conditional when an expression is required. $sample = <expression>; just provides a shortcut when u have 2 results based on an expression, and returns either the true expression or false expression depending on the test_expression. if($gender=='m') $display='Male'; else $display='Female'; see all the repetition shorten with $display=($gender=='m'?'Male':'Female') Quote Link to comment Share on other sites More sharing options...
revraz Posted February 5, 2008 Share Posted February 5, 2008 http://www.ilovejackdaniels.com/php/ternary-conditionals/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.