Naez Posted March 3, 2008 Share Posted March 3, 2008 (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') Could someone explain to me the function of a ? and : in PHP, or perhaps direct me to the page about it on PHP.net site. I'm having trouble finding out. Link to comment https://forums.phpfreaks.com/topic/94136-and-in-php/ Share on other sites More sharing options...
fert Posted March 3, 2008 Share Posted March 3, 2008 http://en.wikipedia.org/wiki/%3F: Link to comment https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482222 Share on other sites More sharing options...
Baabu Posted March 3, 2008 Share Posted March 3, 2008 Well this is called ternary opreator works like If then else consider this <?php ( $a>=$b ) ? echo"$a is greater then $b " : echo "$a is less then b"; ?> well before "?" anycondition is placed which could be either true or false if the condition is true then it will come to the immediate next part mean after "?" and if false then it will go to ":" part hope this makes clear Cheers Link to comment https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482225 Share on other sites More sharing options...
Naez Posted March 3, 2008 Author Share Posted March 3, 2008 Ah, so it is used to simplify small if-else statements. that's pretty neat (and handy) Link to comment https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482228 Share on other sites More sharing options...
aschk Posted March 3, 2008 Share Posted March 3, 2008 Yes it's pretty much an if/else with assignment. It is handy, but sometimes confusing for other developers to read Link to comment https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482288 Share on other sites More sharing options...
svivian Posted March 3, 2008 Share Posted March 3, 2008 It's very useful for printing stuff. Baabu's code can be rewritten as: echo ( $a >= $b ) ? "$a is greater then $b " : "$a is less then b"; Tip: don't ever nest ternary operators, it gets way too confusing to read. And try to keep everything simple - if you're outputting long strings use if/else instead. Link to comment https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.