How to unset global variables.

Samuel Lopez picture Samuel Lopez · Dec 24, 2011 · Viewed 8.4k times · Source

I have an id of a project and an id of a client that are sessions in php that are passed in JSON format. These are stored in global variables id_p and id_c so I can do multiple inserts and updates selects etc. with those ids.

When the user selects another project or changes the page, I need to unset these variables.

  • Can I pass a null value from php to the global vars to reset them?
  • Is there a better way to do what I want?
  • How could I store the php values on php only if the file is required? The files where the queries take place are on separate files.

Answer

luckydonald picture luckydonald · May 12, 2017

To really unset them, use

unset ( $GLOBALS['id_p'] );

This also works in functions.

Source: http://toscho.de/2012/php-unset-unterschied-global-globals/