Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using empty does not provide correct results

Tags:

php

Can someone please tell me why I'm able to echo inside of this block when the session clearly has a value?

$_SESSION['test']['testing'] = 'hgkjhg';
echo $_SESSION['test']['testing']; // Produces hgkjhg (Clearly not empty)

if(empty($_SESSION['test']['testing'])){
echo 'Hello'; // This echoes and to me, shouldn't
}
like image 254
jim Avatar asked Nov 13 '22 23:11

jim


1 Answers

The real answer is about session_start. Unlike session_register, assigning directly to $_SESSION does not automatically call session_start.

Directly from PHP manual for session_register()

If session_start() was not called before this function is called, an implicit call to session_start() with no parameters will be made. $_SESSION does not mimic this behavior and requires session_start()

like image 110
dragonjet Avatar answered Dec 28 '22 15:12

dragonjet