session_start(); if(isset($_SESSION['my_set'])) { // retrieve existing set $set = $_SESSION['my_set']; } else { // create new set $set = array(); } // check if value already exists in set if(!in_array('new_value', $set)) { // add new value to set $set[] = 'new_value'; // store updated set in session $_SESSION['my_set'] = $set; }
use PhpSession\SessionFactory; $session = SessionFactory::getSession(); $set = $session->get('my_set', array()); if(!in_array('new_value', $set)) { $set[] = 'new_value'; $session->set('my_set', $set); }In this example, we are using a PHP class library called `PhpSession` to simplify the session management code. We first create a new session object using the `getSession` static method of the `SessionFactory` class. We then retrieve the existing set from the session using the `get` method of the session object. If `my_set` doesn't exist in the session, we create a new empty array using the second argument of the `get` method. We then check if the new value `'new_value'` already exists in the set, and if it doesn't, we add it to the set and store it back in the session using the `set` method of the session object. In conclusion, the `PhpSession` class library is used in the second example to simplify the session management code, but it is not a built-in PHP package. The `add_to_set` function is not a built-in PHP function either, but rather a custom function that you would need to define yourself if you wanted to use it.