note 111081 added to function.preg-replace-callback
| From: | admin at french-web-proxy dot fr | Date: | Sat, 12 Jan 2013 12:08:12 +0000 |
| Subject: | note 111081 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192991@lists.php.net to get a copy of this message | ||
I Would add a note about varable scope in a function created threw a runetime created function...
<?php
//for exemple
$replace_text='bar';
$foo='foo';
$replace_text=preg_replace_callback('#bar#',
create_function('$result','
global $foo;
$foo=$result; //set variable to bar
//$foo is destroyed here
')
,$replace_text);
echo $foo; //print 'foo' and not 'bar'
?>
the solution is to use superglobals variables...for example $_SERVER
<?php
$replace_text='bar';
$_SERVER['foo']='foo';
$replace_text=preg_replace_callback('#bar#',
create_function('$result','
//global $_SERVER["foo"]; <--don t need this
$_SERVER["foo"]=$result; //set variable to bar
')
,$replace_text);
echo $_SERVER['foo']; //print 'foo' and not 'bar'
?>
here it is ;)
----
Server IP: 69.147.83.199
Probable Submitter: 87.88.65.39
----
Manual Page -- http://php.net/manual/en/function.preg-replace-callback.php
Edit -- https://master.php.net/note/edit/111081
Del: integrated -- https://master.php.net/note/delete/111081/integrated
Del: useless -- https://master.php.net/note/delete/111081/useless
Del: bad code -- https://master.php.net/note/delete/111081/bad+code
Del: spam -- https://master.php.net/note/delete/111081/spam
Del: non-english -- https://master.php.net/note/delete/111081/non-english
Del: in docs -- https://master.php.net/note/delete/111081/in+docs
Del: other reasons-- https://master.php.net/note/delete/111081
Reject -- https://master.php.net/note/reject/111081
Search -- https://master.php.net/manage/user-notes.php