note 113315 added to session.examples.basic

From: Date: Wed, 25 Sep 2013 16:01:04 +0000
Subject: note 113315 added to session.examples.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-196377@lists.php.net to get a copy of this message
Despite the warning about not being able to use references inside the session space, I've done it in the past and apparently completely successfully. That is, I can do something like this: <?php session_start(); if (!$_SESSION['favorite']) { $_SESSION['cow'] = "Elsie"; $_SESSION['favorite'] =& $_SESSION['cow']; echo "We set cow = '$_SESSION[cow]' and favorite =& cow ($_SESSION[favorite]).<br/>Reload the page to see if both change when one changes later.<br/>"; } else { echo "Having re-entered the session after initial settings were made: cow = $_SESSION[cow] and favorite = $_SESSION[favorite].<br/>"; $_SESSION['cow'] = "Bessie"; echo "We reassigned cow = $_SESSION[cow] and our restored reference variable favorite = $_SESSION[favorite]<br/>Note the presence of the &s in the var_dump below.<pre>"; var_dump($_SESSION); echo "</pre><br/>If you reload, the test will begin again."; unset($_SESSION['cow'], $_SESSION['favorite']); session_destroy(); } ?> yields the following after the second request: Having re-entered the session after initial settings were made: cow = Elsie and favorite = Elsie. We reassigned cow = Bessie and our restored reference variable favorite = Bessie Note the presence of the &s in the var_dump below. array(2) { ["cow"]=> &string(6) "Bessie" ["favorite"]=> &string(6) "Bessie" } If you reload, the test will begin again. I've found this ability very useful and storage efficient in the session data. ---- Server IP: 69.147.83.199 Probable Submitter: 70.123.131.58 ---- Manual Page -- http://php.net/manual/en/session.examples.basic.php Edit -- https://master.php.net/note/edit/113315 Del: integrated -- https://master.php.net/note/delete/113315/integrated Del: useless -- https://master.php.net/note/delete/113315/useless Del: bad code -- https://master.php.net/note/delete/113315/bad+code Del: spam -- https://master.php.net/note/delete/113315/spam Del: non-english -- https://master.php.net/note/delete/113315/non-english Del: in docs -- https://master.php.net/note/delete/113315/in+docs Del: other reasons-- https://master.php.net/note/delete/113315 Reject -- https://master.php.net/note/reject/113315 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196377) next »