Jump to content

How can I unset a session variable that is passed as function parameter?


colap

Recommended Posts

<?php    
    if(session_id() == '') {
        session_start();
    }
    $_SESSION['msg']="Updated.";
    psession($_SESSION['msg']);
    function psession($msg){
        echo $msg;
        unset($msg);
    }
?>

This doesn't unset $_SESSION['msg']. How can I unset it?

Link to comment
Share on other sites

Ok, so first things first -- why?

 

if(session_id() == '') {
        session_start();
    }
Do you want sessions or not? If so, you should just have session_start().

 

 

$_SESSION is a super global.

 

In PHP superglobals are variables that are available everywhere and thus visible inside functions.

 

 

So, in this case, your function has little to no value in terms of the passing of a parameter. Quite frankly, it would be really bad practice to do what you were trying to do even if it did work.

 

One purpose of a functions is to hide details from the rest of the program, so setting/unsetting a variable inside a function that is otherwise global really goes against the idea of "information hiding".

Link to comment
Share on other sites

Ok, so first things first -- why?

 

if(session_id() == '') {
        session_start();
    }
Do you want sessions or not? If so, you should just have session_start().

 

I've done that in a handful of cases where I needed sessions but wasn't sure if it had been started yet (and there wasn't anywhere that would authoritatively start them). It could be an indication of poor design, however.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.