What is the default value of a session variable?
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;
echo "views = ". $_SESSION['views'];
?>
Is $_SESSION['views']
initialized as FALSE
?
EDIT: I meant the third line of code:
if(iss开发者_运维百科et($_SESSION['views']))
There is nothing special with $_SESSION['views']
. $_SESSION
is a "normal" array. If you don't set anything, views
is just an undefined array index.
It's initialised as 1
, because you told it to be.
$_SESSION['views']
is initialized as whatever you set it as. According to your example, it would be initialized as 1
or TRUE
.
If I understand correctly, it's like this: if the user has never been to your site, then you are starting a new session and $_SESSION['views']
will not be set (so you set it to 1). On the other hand if the user's request comes with a session ID of a valid session, then the variable will be set to whatever it was last stored as.
精彩评论