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; } ?? Link to comment https://forums.phpfreaks.com/topic/89595-solved-very-quick-question/ 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'; ?> Link to comment https://forums.phpfreaks.com/topic/89595-solved-very-quick-question/#findComment-458995 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') Link to comment https://forums.phpfreaks.com/topic/89595-solved-very-quick-question/#findComment-459001 Share on other sites More sharing options...
revraz Posted February 5, 2008 Share Posted February 5, 2008 http://www.ilovejackdaniels.com/php/ternary-conditionals/ Link to comment https://forums.phpfreaks.com/topic/89595-solved-very-quick-question/#findComment-459005 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.