Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.