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 Quote Link to comment 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. Quote Link to comment 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 ..."; Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 23, 2008 Share Posted September 23, 2008 $a = "content ".(($something != 0)? "print!" : ""). "continue ..."; Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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). Quote Link to comment 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. Quote Link to comment 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"; Quote Link to comment 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. 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.