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 ?