tags:

views:

39

answers:

3

Hey Guys,

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 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

+5  A: 

isset

Sjoerd
+4  A: 

Try this:

if (!isset($_SESSION['second_prefix'])){
  $_SESSION['second_prefix'] = "";
}
jigfox
+2  A: 

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 )

Peter Ajtai