W3docs

How to remove a variable from a PHP session array

To remove a variable from a PHP session array, you can use the unset() function.

To remove a variable from a PHP session array, you can use the unset() function. Here's an example:

Example of using unset() function to remove a variable from a session array in PHP

<?php

// Start the session
session_start();

// Remove a variable from the session array
unset($_SESSION['var_name']);

This will remove the variable 'var_name' from the session array. If you want to remove multiple variables, you can pass them as additional arguments to unset(), like this:

Example of using unset() function to remove multiple variables from a session array in PHP

<?php

unset($_SESSION['var_name1'], $_SESSION['var_name2'], $_SESSION['var_name3']);

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

Note: Do not use array_splice() for $_SESSION. It is designed for numeric/indexed arrays and will reindex elements, which breaks the associative key-value structure of session data. Also, be careful not to confuse unset($_SESSION['key']) with session_unset(), which removes all variables from the current session.