Dysan Posted November 29, 2007 Share Posted November 29, 2007 Why doesn't the following code work. I'm trying to test if an array exists, to insert data into it, else create the array. Only each time I access the page, the array contains no data. Why is this? <?php session_start(); $id = $_GET['id']; $array = $_SESSION['ids']; if (!isset($array)) { $array = array(); } else { $array[] = 'Data'; $_SESSION['ids'] = $array; } echo $_SESSION['ids']; print_r($array); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 29, 2007 Share Posted November 29, 2007 You have problems with your logic. I think this will do what you're trying to do: <?php session_start(); $id = (isset($_GET['id']))?$_GET['id']:''; $array = (isset($_SESSION['ids']))?$_SESSION['ids']:array(); $array[] = $id; $_SESSION['ids'] = $array; echo '<pre>$_SESSION:' . print_r($_SESSION['ids'],true) . '</pre>'; echo '<pre>$array:' . print_r($array,true) . '</pre>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.