Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP:: Best way to detect if session start

I was wondering if this if statement :

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

is equivalent to this if statement :

if(!session_id()){}

Both works for me!

but I like the sorter line so I was just wondering if it's 100% equivalent and I can rely on that to detect if session is started.

like image 315
Hezi-Gangina Avatar asked May 13 '15 10:05

Hezi-Gangina


1 Answers

Those statements are not fully equivalent.

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

and if(session_id()===""){} is checking if session_id() is returning empty STRING, so the only option, for which the if statement will return TRUE, is ''.

From PHP Manual about session_id():

session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).

So, the preferable is to use the 1st way.

like image 65
Jazi Avatar answered Sep 23 '22 18:09

Jazi