Luis Gerardo Runge Luis Gerardo Runge - 1 year ago 97
PHP Question

Release or Clean php variables after require_once

Just a quick question (I Hope)
I'm calling two PHP script using require_once, those script have some variables in PHP, JS, etc.

<?php require_once "page1.php";?>
<?php require_once "page2.php";?>

The issue is that page2.php have some variables with the same name in page1.php but with different values

Is there any way to require page1.php, show the content and after "release" or "clean" the content of variable in memory in order to require page2.php without conflics?

I know, will be more simple to change name of variables... But, I need ask ;)

Thanks for your help

Answer Source

may be you are looking for this ->

require_once "page1.php";

//flush all variables of page1

$vars = array_keys(get_defined_vars());
for ($i = 0; $i < sizeOf($vars); $i++) {
require_once "page2.php";

And to clarify, implode returns "a string representation of all the array elements in the same order".

Unset requires the actual variable as a parameter, not just a string representation. Which is similiar to what get_defined_vars() returns (not the actual variable reference). So the code goes through the array of strings, and returns each as a reference using the extra $ in front - which unset can use.