3

I do this to test and get session attribute in my code :

if ($session->has("myVar")) {
    $myVar = session->get("myVar");
    /* do something */ 
}

But, I read the code of session and :

 /**
 * Returns an attribute.
 *
 * @param string $name    The attribute name
 * @param mixed  $default The default value if not found.
 *
 * @return mixed
 *
 * @api
 */
public function get($name, $default = null);

So, if the session attribute is not find, get return "null".

if ($myVar = $session->get("myVar")) {
    /* do something */    
}

Or better with false, if you have "myVar" but empty, you can't rely on "null" :

if ($myVar = $session->get("myVar", false)) {
    /* do something */    
}

according to : Null vs. False vs. 0 in PHP

I think the third is the best, one call to $session->get, instead of has and get in my actual code.

I read about alternative comparaison with ternary operator, http://fabien.potencier.org/article/48/the-php-ternary-operator-fast-or-not, I don't use it, mostly because I.m not familiar with, but, if it's not faster, I don't need it.

Can you confirm, third is the best, and if it's not, why, and how to do best ?

Community
  • 1
  • 1
Nicolas
  • 571
  • 6
  • 13
  • 1
    What exactly do you want to test it for? That is exists? That is exists, but doesn't have a value? That it exists but the value is not the one you want? – Andrei Jun 04 '15 at 10:20
  • I have to be sure the session attribute is set, and get is value – Nicolas Jun 04 '15 at 10:56

1 Answers1

4

That depends on what you want to achieve.

First Option

if ($session->has("myVar")) {
    $myVar = session->get("myVar");
    /* do something */ 
}

Makes sense if you want to make sure, that this var is set, and if not you actually want to set it or do anything else. It presumes that having this session variable present is a necessity

Second Option

$myVar = $session->get("myVar");

This one makes sense, if you are comfortable receiving the expected result or a null value as default if not set.

Recommendation:

$myVar = $session->get("myVar");
if (null === $myVar) {
    /* do something */    
}

Third Option

$myVar = $session->get("myVar", false);

This one simply allows you to override the default value if this session variable is not set

Recommendation:

$myVar = $session->get("myVar", false);
if (!$myVar) {
    /* do something */    
}

So, in my opinion it depends what you want to do. But there is no best or worst way as you asked for, since they all differ from each other.

DerStoffel
  • 2,553
  • 2
  • 15
  • 25
  • My principal interrogation is : if I do the first I use two call (has then get) If I use the third, I've the same result, but one call (just get). Both are correct, and beside coding style, I don't know if one is the best no matter what. – Nicolas Jun 04 '15 at 11:00
  • all are fine. They just depend on what you want. But that is answered in my last paragraph :) – DerStoffel Jun 04 '15 at 11:01
  • Though this is an old topic, but how about: if ($session->has('parameter') && $session->get('parameter') !== null) { do something; } – Ali Apr 13 '20 at 15:55
  • sure, why not. Always depends on how many session hits you want to have. did you profile it? – DerStoffel Dec 31 '21 at 11:07