Jump to content

What does the "?" mean in php code


eldan88

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/260827-what-does-the-mean-in-php-code/
Share on other sites

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 :) 

 

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?

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.