note 100689 added to function.include

From: Date: Sun, 31 Oct 2010 15:49:20 +0000
Subject: note 100689 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-173616@lists.php.net to get a copy of this message
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 ---- Server IP: 195.54.192.44 Probable Submitter: 127.0.0.1 ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- https://master.php.net/note/edit/100689 Del: integrated -- https://master.php.net/note/delete/100689/integrated Del: useless -- https://master.php.net/note/delete/100689/useless Del: bad code -- https://master.php.net/note/delete/100689/bad+code Del: spam -- https://master.php.net/note/delete/100689/spam Del: non-english -- https://master.php.net/note/delete/100689/non-english Del: in docs -- https://master.php.net/note/delete/100689/in+docs Del: other reasons-- https://master.php.net/note/delete/100689 Reject -- https://master.php.net/note/reject/100689 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173616) next »