Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to unset a session variable?

Tags:

php

I have the following code, but it does not work, I don't know what mistake I am committing due to which this code does not work:

Here is the code:

<?php session_start();
if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']) && !empty($_GET['indexNo'])) 
{
   $indx = $_GET['indexNo'];
   foreach($_SESSION['itemsOrder'] as $key => $val)
{
 echo "$key => $val <br> " ;    
  if($indx == $val)
  {
    unset($_SESSION['itemsOrder'][$val]);

  }
  else
  {

    echo "indexNo was not unset <br>";
  }
 }
}
else 
{
    echo "indexNo not received!";
}
?>
like image 643
Atiq Ahmad Avatar asked Feb 10 '26 06:02

Atiq Ahmad


1 Answers

Should be $key not the $val . Try with -

if($indx == $key)
{
    unset($_SESSION['itemsOrder'][$key]);

}

No need to use isset & empty together.

if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']))
like image 185
Sougata Bose Avatar answered Feb 13 '26 08:02

Sougata Bose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!