PHP :: Best way to determine if a session is running

I was wondering if this is an if statement:

if(session_id()===""){} 

equivalent to this if statement:

 if(!session_id()){} 

Both work for me!

but I like the sorter line, so I'm just wondering if it is 100% equivalent, and I can rely on this to determine if the session is running.

+5
source share
1 answer

These statements are not completely equivalent.

if(!session_id()){} means if(session_id() != TRUE){} , so the function session_id() can return 0 , FALSE , '' , NULL .

and if(session_id()===""){} checks if session_id() returns an empty STRING, so the only option for which the if returns TRUE is '' .

From the PHP manual about session_id() :

session_id () returns the session identifier for the current session or an empty string ("") if the current session is absent (current session is absent id exists).

So, it is preferable to use the 1st method.

+7
source

All Articles