note 90021 added to function.preg-replace-callback

From: Date: Thu, 02 Apr 2009 07:25:13 +0000
Subject: note 90021 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-152737@lists.php.net to get a copy of this message
When you access variables from outside in a callback function, use the $global keyword: <?php // global # 1: global $x; $x = 0; $str = '&Bla bla. &#x25ba;'; $find = '/(\&)([^#])/'; // global # 2: $replace = create_function('$f', 'global $x; $x ++; return $f[2];'; $str2 = preg_replace_callback($find, $replace, $str); // $x == 1 // $str2 == 'Bla bla. &#x25ba;' // without global, $x would be 0 ?> ---- Server IP: 217.73.17.12 Probable Submitter: 91.127.12.245 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- https://master.php.net/note/edit/90021 Del: integrated -- https://master.php.net/note/delete/90021/integrated Del: useless -- https://master.php.net/note/delete/90021/useless Del: bad code -- https://master.php.net/note/delete/90021/bad+code Del: spam -- https://master.php.net/note/delete/90021/spam Del: non-english -- https://master.php.net/note/delete/90021/non-english Del: in docs -- https://master.php.net/note/delete/90021/in+docs Del: other reasons-- https://master.php.net/note/delete/90021 Reject -- https://master.php.net/note/reject/90021 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152737) next »