note 100689 deleted from function.include by danbrown
| From: | danbrown@php.net | Date: | Sun, 31 Oct 2010 20:55:24 +0000 |
| Subject: | note 100689 deleted from function.include by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173619@lists.php.net to get a copy of this message | ||
Note Submitter: phatum at mail dot ru
----
I made some modifications in my 'include_text' function
and now it imports global vars before execution and
prevents function parameter from impotring and exporting
to the global.
Also I added here more complicated example to show that
this trick works fine even with mixed php+html code and
"<<<". So it seems to be universal...
<?php
function include_text($include_text_param12345) {
// preventing function param from importing from the global
unset($GLOBALS['include_text_param12345']);
// importing global vars
extract($GLOBALS);
// executing script
eval ("?>$include_text_param12345");
// preventing function param from exporting to the global
unset($include_text_param12345);
// exporting global vars
$GLOBALS += get_defined_vars();
}
$WORLD = "world"; // global var
$txt = '<? $a = "1"; $b = "2";' // local vars become global
.' if($a==1) { ?>Hello<? } else { ?>Goodbye<? };' // php-html mix
.' echo <<<EOT' // <<< operator
."\n, \$WORLD!<br>" // global var INSIDE the script
."\nEOT;\n?>--------<br>";
include_text ($txt); // including the script
// vars exported from the script
echo "a = $a<br>";
echo "b = $b<br>";
?>
Output:
Hello, world!
--------
a = 1
b = 2