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); ?> Link to comment https://forums.phpfreaks.com/topic/79471-doesnt-work/ 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 Link to comment https://forums.phpfreaks.com/topic/79471-doesnt-work/#findComment-402410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.