开发者

using empty does not provide correct results

开发者 https://www.devze.com 2023-03-04 03:15 出处:网络
Can someone please tell me why I\'m able to echo inside of this block when the session clea开发者_StackOverflow社区rly has a value?

Can someone please tell me why I'm able to echo inside of this block when the session clea开发者_StackOverflow社区rly 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
}


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


Try commenting out the rest of your code. Something is either hanging in the session, or you're assigning values elsewhere in your script.

<?php 
session_start();

$_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
}

var_dump($_SESSION);
?>

Results:

hgkjhgarray(1) { ["test"]=> array(1) { ["testing"]=> string(6) "hgkjhg" } }

Just for argument's sake, you are saying you see "Hello" rather than "hgkjhg". "hgkjhg" should be displayed.


Try changing it into:

$_SESSION['test']['testing'] = 'hgkjhg';
echo $_SESSION['test']['testing']; 

if(empty($_SESSION['test']['testing'])){
    echo 'Hello. Error in $_SESSION !'; 
}

If it is still echoing "Hello", then you have somewhere later in your code a line with:

echo 'Hello';
0

精彩评论

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