redarrow Posted March 6, 2007 Share Posted March 6, 2007 Advance thank you as i have been learning php for a while i fort i get a new php 5 bible book to see what i need to brush up on there are 45 chapters and i am on chapter 6 i need your help cheers. I am learning the tenery operator from the below example but the result is always true why? Can you clear this opertor up so i understand it properly. // ? means if // : means is true <?php $a=10; $b=20; if($a === $b ? $a : $b ){ echo " correct a is larger then b"; }else{ echo " not correct a is not larger then b"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/ Share on other sites More sharing options...
kenrbnsn Posted March 6, 2007 Share Posted March 6, 2007 The problem here is that you are using the triple equal sign, "===", which means the condition is true if the types of the arguments are the same. The tenery operartor is a short way of doing an "if-else" block. The next two snippets of code do the same thing: <?php $a = 10; $b = 20; $result = ($a == $b)?$a:$b; echo $result; ?> <?php $a = 10; $b = 20; if ($a == $b) $result = $a; else $result = $b; echo $result; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200970 Share on other sites More sharing options...
.josh Posted March 6, 2007 Share Posted March 6, 2007 well okay, that's sort of true, but not entirely relevant here.. the problem redarrow is that yes, a ternary operator is a condition. So the problem here is that you have a condition nested inside a condition. A condition will evaluate true or false. Look at your code: <?php $a=10; $b=20; if($a === $b ? $a : $b ){ echo " correct a is larger then b"; }else{ echo " not correct a is not larger then b"; } ?> You assign a value to $a and $b first. Next you have an 'if' condition. 'if' what? inside your 'if' statement you do a ternary operation. if $a === $b return $a if it's true, $b if it's false. So either way, it will return a value, since you assigned a value to both variables. Therefore, your original 'if' statement will always look like one of the two: if($a) { ...} or if($b) { ... } since you assigned something to both of them in the first place, it will always return true. edit: the point of the ternary operator is, as ken stated: a shorthand if..else statement. if(true) { $blah = something; else { $blah = something else; } can be shortened to a ternary operator like this: $blah = (true) ? something : something else; example: $x = 1; $blah = ($x == 1) ? "one" : "not one"; Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200986 Share on other sites More sharing options...
boo_lolly Posted March 6, 2007 Share Posted March 6, 2007 a sample of the way i do it: (($a == $b) ? ("print something") : ("print something else")); Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200988 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2007 Share Posted March 6, 2007 The OP edited his original example while I was typing my response which is why it doesn't relate. His original example was <?php $a=10; $b=20; $result = $a === $b ? $a : $b; if ($result) { echo " correct a is larger then b"; }else{ echo " not correct a is not larger then b"; } ?> Which is not correct either. Ken Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200989 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 I done it this way ? Thank you all for your support and examples cheers. <?php $a=100; $b=200; $c=900; $d=1000; $result=($a >$b)?$a:$b?$c:$b?$d:$a; echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200991 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 i would like to no how this relates to programming the long way in speed or is all the ternary operator for programmers prefrence in style or is there a reel reason to use this style of programming? Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200997 Share on other sites More sharing options...
artacus Posted March 6, 2007 Share Posted March 6, 2007 I've never seen that before. I don't think it works. I've tried it and it always evaluates to $d. If it does work, its not at all clear what it is doing. Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-200999 Share on other sites More sharing options...
artacus Posted March 6, 2007 Share Posted March 6, 2007 is there a reel reason to use this style of programming? I use it often for little things like this: $css = ($i++ % 2) ? 'class="odd"' : 'class="even"'; Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-201003 Share on other sites More sharing options...
boo_lolly Posted March 6, 2007 Share Posted March 6, 2007 i would like to no how this relates to programming the long way in speed or is all the ternary operator for programmers prefrence in style or is there a reel reason to use this style of programming? i use it for small if statements. and to keep my code tidyer. <?php $sql = "SELECT * FROM my_table"; $query = mysql_query($sql); echo "<select name=\"drop_down\">\n"; while($row = mysql_fetch_array($query)){ echo "<option value=\"". $row['column1'] ."\"". (($row['column1'] == $_POST['drop_down']) ? (" SELECTED") : ("")) .">". $row['column2'] ."\n"; } echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-201005 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2007 Share Posted March 6, 2007 if you write that as <?php $a=100; $b=200; $c=900; $d=1000; $result=((($a >$b)?$a:$b)?$c:$b)?$d:$a; ?> Here is what is happening: ($a>$b) is evaluated first and is "false", so the expression's result is "$b" or "200". 200 is evaluated as "true", so the next part's result is "$c" or 900. 900 is also evaluated as "true", so the final answer is "$d" or 1000. Ken Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-201006 Share on other sites More sharing options...
redarrow Posted March 6, 2007 Author Share Posted March 6, 2007 Is this the correct way thank you all for your examples cheers. <?php $a=100; $b=200; $c=900; $d=1000; $result=(($a>$b)||($c>$d))? "true" : "not true" ? "true" : "not true" ; echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-201017 Share on other sites More sharing options...
.josh Posted March 7, 2007 Share Posted March 7, 2007 redarrow, it mostly just boils down to preference. That's why it's called shorthand. Like the others, I use it for short things. Mostly if I have a variable that's going to be assigned something based on either one thing or the other, and that's all that needs to be done for that condition, I simply use the ternary operator. I wouldn't recommend nesting ternary operators like that. If for no other reason, because it can easily become confusing to you, the programmer. You want to strive for elegant coding, but part of elegance is clarity and ease of reading (by humans). Quote Link to comment https://forums.phpfreaks.com/topic/41482-learning-ternery-operator/#findComment-201457 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.