note 21140 modified in language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:43:24 +0000 |
| Subject: | note 21140 modified in language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155587@lists.php.net to get a copy of this message | ||
Seems as though when a cookie is saved and referenced as a variable of the same name as the cookie,
that variable is NOT global. If you make a function ro read the value of the cookie, the cooke
variable name must be declared as a global.
example:
<?php
function ReturnCookie()
{
$cookieName = "Test_Cookie";
global $$cookieName;
if (isset($$cookieName))
{
echo ("$cookieName is set");
$returnvalue = $$cookieName;
}
else
{
$newCookieValue = "Test Value";
setcookie("$cookieName","$newCookieValue", (time() + 3153600));
echo ("made a cookie:" . $newCookieValue ."<BR>");
$returnvalue = $newCookieValue;
}
echo ("the cookie that was set is now $returnvalue <BR>");
return $returnvalue;
}
?>
--was--
Seems as though when a cookie is saved and referenced as a variable of the same name as the cookie,
that variable is NOT global. If you make a function ro read the value of the cookie, the cooke
variable name must be declared as a global.
example:
function ReturnCookie()
{
$cookieName = "Test_Cookie";
global $$cookieName;
if (isset($$cookieName))
{
echo ("$cookieName is set");
$returnvalue = $$cookieName;
}
else
{
$newCookieValue = "Test Value";
setcookie("$cookieName","$newCookieValue", (time() + 3153600));
echo ("made a cookie:" . $newCookieValue ."<BR>");
$returnvalue = $newCookieValue;
}
echo ("the cookie that was set is now $returnvalue <BR>");
return $returnvalue;
}
http://php.net/manual/en/language.variables.scope.php