开发者

How to create a session variable if none exists prior, but ignore if variable exists but is blank

开发者 https://www.devze.com 2023-01-04 11:36 出处:网络
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.

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 )

0

精彩评论

暂无评论...
验证码 换一张
取 消