Maq Posted September 10, 2008 Share Posted September 10, 2008 I've been teaching myself PHP and have seen people using the syntax, "? :". I assume it is an alternative for a condition statement but I have been searching Google and can't find an explanation. Example: $port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : ''; Please explain. Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/ Share on other sites More sharing options...
effigy Posted September 10, 2008 Share Posted September 10, 2008 Ternary Operator Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638485 Share on other sites More sharing options...
Maq Posted September 10, 2008 Author Share Posted September 10, 2008 This syntax is not listed in the link provided but I know what it means now. In the example that I gave it means: If there is a port $port = $_SERVER["SERVER_PORT"]; If there isn't a port then $port = ''; Correct me if I'm wrong but, this is just a shortcut for IF/ELSE statement. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638497 Share on other sites More sharing options...
lanmonkey Posted September 10, 2008 Share Posted September 10, 2008 yes Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638499 Share on other sites More sharing options...
effigy Posted September 10, 2008 Share Posted September 10, 2008 This syntax is not listed in the link provided From the link: The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638500 Share on other sites More sharing options...
Mchl Posted September 10, 2008 Share Posted September 10, 2008 Correct me if I'm wrong but, this is just a shortcut for IF/ELSE statement. Not really but works in similar way. You can't do (mysql_connect($a,%b,$c,$d)) ? {$query = "query"; mysql_query($query); etc..} : {echo mysql_error();} Ternary operator is best used, where you want to assign a value to a variable according to some condition. For example echo ($mysql_query("INSERT INTO ...")) ? "Data saved" : "Error: ".mysql_error(); Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638501 Share on other sites More sharing options...
effigy Posted September 10, 2008 Share Posted September 10, 2008 You can't do (mysql_connect($a,%b,$c,$d)) ? {$query = "query"; mysql_query($query); etc..} : {echo mysql_error();} Because, per the documentation, those are not valid expressions. $query = "query" && mysql_query($query) might work, but then you're entering into a realm of sloppy code. Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638504 Share on other sites More sharing options...
Mchl Posted September 10, 2008 Share Posted September 10, 2008 There was this contest for worst code... let me see if I saved the url.... Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638511 Share on other sites More sharing options...
Maq Posted September 10, 2008 Author Share Posted September 10, 2008 Hey sorry effigy, I only looked at the Comparison Operators table (obviously) and didn't think it was there. Ternary operator is best used, where you want to assign a value to a variable according to some condition. I see what you mean Mchl. Just like in my example, you're assigning port to the SERVER_PORT if it exists but if not then assign nothing. Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638516 Share on other sites More sharing options...
The Little Guy Posted September 10, 2008 Share Posted September 10, 2008 $a = 1; if($a == 1){ echo 'a is one!' }else{ echo 'a is not one '; } // Is the same as: $a = 1; echo ($a == 1)? 'a is one!' : 'a is not one '; Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638517 Share on other sites More sharing options...
Maq Posted September 10, 2008 Author Share Posted September 10, 2008 Thanks The Little Guy. This, yet again, confirms the concept of the ?: ternary operator for me. Is there a name for this type of ternary operation? Quote Link to comment https://forums.phpfreaks.com/topic/123644-solved-what-does-it-mean/#findComment-638521 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.