How to create a session variable if none exists prior, but ignore if variable exists but is blank
I have this problem. I want to detect if there is not a session variable and create one, but if one already exists and is empty I want to leave it alone.
Say开发者_StackOverflow中文版 I have this to create my session variable if none is detected:
if (!$_SESSION['second_prefix']){$_SESSION['second_prefix'] = "";}
How should I change this to not perform any action is $_SESSION['second_prefix'] exists but is purposely blank?
Hudson
Try this:
if (!isset($_SESSION['second_prefix'])){
$_SESSION['second_prefix'] = "";
}
isset
isset() will do this for you.
The important thing about if(isset($blah)) as opposed to if($blah) is the isset() will be true even if $blah is empty. In other words, for your case, $_SESSION['second_prefix']=""
However, isset() will return false if $blah is NULL. So, while $blah="" is fine, $blah=NULL would return false for isset($blah).
( is_null() will check if a variable is NULL )
加载中,请稍侯......
精彩评论