PureEvil Posted July 20, 2006 Share Posted July 20, 2006 I've pretty much learned all of my PHP from a few books I'ver purchased and of course this kick ass web site.The question is this. I keep seeing code like the following[code]$row_color = ($Rcount % 2) ? $Talt_color1 : $Talt_color2;[/code]What does the ? and the : mean in that string? I've been unable to find these in my books.Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/ Share on other sites More sharing options...
spires Posted July 20, 2006 Share Posted July 20, 2006 I'm in the same boat. every book or CD that i have used dosen't explain ? or :Can anyone explin exactly what this line of code does?[code] foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; }[/code]Cheers Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61047 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 It's an if/else statement I believe.(if) ? true : false (but I might be wrong on the order.) Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61050 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 Aha! From the manual:[code]Left associativity means that the expression is evaluated from left to right, right associativity means the opposite. Example 15-1. Associativity<?php$a = 3 * 3 % 5; // (3 * 3) % 5 = 4$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2$a = 1;$b = 2;$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5?> Use parentheses to increase readability of the code. [/code]http://us2.php.net/manual/en/language.operators.php Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61051 Share on other sites More sharing options...
spires Posted July 20, 2006 Share Posted July 20, 2006 cheers, I'll try and get my head around it. ;D Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61054 Share on other sites More sharing options...
PureEvil Posted July 20, 2006 Author Share Posted July 20, 2006 [quote author=ChaosXero link=topic=101251.msg400473#msg400473 date=1153408568]It's an if/else statement I believe.(if) ? true : false (but I might be wrong on the order.)[/quote]Ok I think I'm getting it maybe.Basically in a nutshell your saying that if something is true or false do whats true or false.Example:[code]$i=1;$Var1 = "i is 1";$Var2 = "i is not 1";$Var3 = ( $i == 1 ) ? $Var1 : $Var2;echo("$Var3"); // should produce "i is 1"[/code]Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61142 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 I'm not sure really. I'm at work, so I cant try, but if someone could try/confirm this, I'd be interested to know. Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61144 Share on other sites More sharing options...
PureEvil Posted July 20, 2006 Author Share Posted July 20, 2006 [quote author=ChaosXero link=topic=101251.msg400574#msg400574 date=1153414234]I'm not sure really. I'm at work, so I cant try, but if someone could try/confirm this, I'd be interested to know.[/quote]Seems to work... That is awesome and will come in extra handy. Honest to god I have four php books I pretty much live in while I'm trying to write my scripts, and not one of them said anything about this. Very handy... Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61146 Share on other sites More sharing options...
scliburn Posted July 20, 2006 Share Posted July 20, 2006 yes you are correct, it simplifies an if/else alot like the switch statement$thefunc = ((isset($var)) && ($var != '')) ? _function ( $var, $var2 ) : $content = 'No Word Entered<br /><br />';you can use it call and pass vars through a function and then $thefunc could have the value returned from your functions.. just an example Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61149 Share on other sites More sharing options...
effigy Posted July 20, 2006 Share Posted July 20, 2006 It can also be shortened to:[code]echo $i == 1 ? $Var1 : $Var2;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61151 Share on other sites More sharing options...
ryanlwh Posted July 20, 2006 Share Posted July 20, 2006 it's a "ternary" operator that's adopted from C like languages. It's really sad to see php books don't cover this operator much. This is a really handy operator. You can even use it in a concatenation.[code]echo 'This is a '.( $good ? 'good' : 'bad'). 'book';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15152-question-about-and/#findComment-61159 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.