eldan88 Posted April 12, 2012 Share Posted April 12, 2012 Hi, I am fairly new to PHP and I have developer who writes my code. I have seen that he has been using the "?" a lot but don't know exactly what it is used for. I have checked the php.net website and couldn't find any documentation on it. Here is an example of how he used the question mark in on the codes $my_cart = isset($_SESSION['my_cart'])?$_SESSION['my_cart']:0; Is this a recommended method of writing the code? What does it mean, and how can I look up more info on it. Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/ Share on other sites More sharing options...
xyph Posted April 12, 2012 Share Posted April 12, 2012 It's the ternary operator http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary Basically, a very simplified if/else statement. Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/#findComment-1336816 Share on other sites More sharing options...
algidDes702 Posted April 12, 2012 Share Posted April 12, 2012 A quick explanation of that example might help as well as reading the documentation: $my_cart = isset($_SESSION['my_cart'])?$_SESSION['my_cart']:0; So basically its starting off with a conditional statement: is $_SESSION['my_cart'] have a value in it or not? the other side of the ? is the value that will be applied depending on if the statement is true or false. If TRUE then it uses the value to the left of the ":" and assigns that to $my_cart. If FALSE then it assigns the value to the right of the ":" which would be 0 which is also equal to FALSE. so statement is true $my_cart = $_SESSION['my_cart']; statement is false $my_cart = 0; Hope for this specific example that helped explain it if the documentation didnt quite make sense, sometimes it confuses my so a little more help is always good Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/#findComment-1336819 Share on other sites More sharing options...
eldan88 Posted April 13, 2012 Author Share Posted April 13, 2012 A quick explanation of that example might help as well as reading the documentation: $my_cart = isset($_SESSION['my_cart'])?$_SESSION['my_cart']:0; So basically its starting off with a conditional statement: is $_SESSION['my_cart'] have a value in it or not? the other side of the ? is the value that will be applied depending on if the statement is true or false. If TRUE then it uses the value to the left of the ":" and assigns that to $my_cart. If FALSE then it assigns the value to the right of the ":" which would be 0 which is also equal to FALSE. so statement is true $my_cart = $_SESSION['my_cart']; statement is false $my_cart = 0; Hope for this specific example that helped explain it if the documentation didnt quite make sense, sometimes it confuses my so a little more help is always good Thanks ! That made a lot more sense now =) Is this method a good programming practice? Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/#findComment-1336884 Share on other sites More sharing options...
kicken Posted April 13, 2012 Share Posted April 13, 2012 Is this method a good programming practice? The example code you provided is a perfectly valid and commonly used application of that operator. Using it for small things like that is just fine. Some people will get a little crazy with it and try and put a ton of stuff into it which just ends up making the code complicated and hard to read. Basically, if used in moderation then it's fine and can even enhance code's readability. If abused though it can make for a nightmare. Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/#findComment-1336887 Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 I personally use it a lot with concatenation. <?php $games = 1; echo 'The children played '.$games.' game'. ( $games > 1 ? 's' : '' ); ?> In the above example, if $games > 1, it will add an 's' to the output, otherwise, nothing. Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/#findComment-1336889 Share on other sites More sharing options...
eldan88 Posted April 13, 2012 Author Share Posted April 13, 2012 I personally use it a lot with concatenation. <?php $games = 1; echo 'The children played '.$games.' game'. ( $games > 1 ? 's' : '' ); ?> In the above example, if $games > 1, it will add an 's' to the output, otherwise, nothing. That make sense xyph . Thanks for the example. Quote Link to comment https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/#findComment-1336915 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.