asmith Posted September 23, 2008 Share Posted September 23, 2008 Hey guys This short hand syntax is killing me. All I read about it on the net is about "echo" examples. why this works : echo "content ",($something != 0)? "print!" : "", "continue ..."; but when i put it in a variable : $a = "content ",($something != 0)? "print!" : "", "continue ..."; it gives me error? How to solve it ? any GREAT If shorthand tutorial? Thanks Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2008 Share Posted September 23, 2008 why this works : Because echo accepts a coma separated lists of parameters and an assignment statement does not. You would need to try the "." (dot) concatenation operator. Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648594 Share on other sites More sharing options...
Mchl Posted September 23, 2008 Share Posted September 23, 2008 You can't do something like that: $a = "content ","print!","continue ..."; If you want to join strings use . (that's a dot) $a = "content ".($something != 0)? "print!" : "". "continue ..."; Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648596 Share on other sites More sharing options...
asmith Posted September 23, 2008 Author Share Posted September 23, 2008 $a = "content ".($something != 0)? "print!" : "". "continue ..."; by echo $a , it gives me "print!". it ignores "content " and "continue ...". It only gives me print!. Any idea? Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648600 Share on other sites More sharing options...
Mchl Posted September 23, 2008 Share Posted September 23, 2008 $a = "content ".(($something != 0)? "print!" : ""). "continue ..."; Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648603 Share on other sites More sharing options...
asmith Posted September 23, 2008 Author Share Posted September 23, 2008 omg! lol just a couple of parenthesis? Thanks man. was driving me crazy! Anyway isn't any good turorial about these shorthand syntaxes? Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648607 Share on other sites More sharing options...
Mchl Posted September 23, 2008 Share Posted September 23, 2008 What other tutorial you need? It just works. Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648631 Share on other sites More sharing options...
wildteen88 Posted September 23, 2008 Share Posted September 23, 2008 omg! lol just a couple of parenthesis? Thanks man. was driving me crazy! Anyway isn't any good turorial about these shorthand syntaxes? The best place is always the manual (NOTE: scroll down to the Ternary Operator section). Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648785 Share on other sites More sharing options...
.josh Posted September 23, 2008 Share Posted September 23, 2008 omg! lol just a couple of parenthesis? Thanks man. was driving me crazy! Anyway isn't any good turorial about these shorthand syntaxes? No not really, because there isn't really anything else that's "shorthand" syntax. Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-648794 Share on other sites More sharing options...
asmith Posted September 28, 2008 Author Share Posted September 28, 2008 @wildteen88 : thanks for the link! This shorthand is only for if and else ? I mean can't be 3 parts, like "elseif" $a = 5; echo "print ",($a == 5)? "it is 5" : ($a == 7)? "777" : "none"," finished"; Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-652302 Share on other sites More sharing options...
.josh Posted September 28, 2008 Share Posted September 28, 2008 you can nest it like this: <?php $y = 10; $x = ($y < 11)? (($y < 11)? "yes" : "innerno") : "outterno"; echo $x; ?> output will be "yes", though seriously, I would not recommend doing this sort of thing. You should never sacrifice readability for a couple less lines of code. Link to comment https://forums.phpfreaks.com/topic/125463-if-shorthand/#findComment-652308 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.