note 105130 added to function.str-replace
| From: | didiergm at osu1 dot php dot net | Date: | Fri, 29 Jul 2011 10:25:08 +0000 |
| Subject: | note 105130 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181991@lists.php.net to get a copy of this message | ||
php 5.3.2 (may exist before, but it never saw it until today)
the replace is always evaluated before the search takes place.
So if like me you use str_replace to replace some macros in the html code, and these replacement
call a function, you must be aware that the functions always runs, not only when the search is
found.
for example:
str_replace (
array ('%macro1%', '%macro2%'),
array (myfunction1(), myfunction2(),
thesource);
myfunction1() and myfunction2() are called regardless of the fact that the source contains
%macro1%' or '%macro2%' or not. I genuinely believed that myfunction1 would only be
called if %macro1% was found (resp myfunction2 and %macro2%)
I noticed it because myfunction1 was running a database query to increment a value and I noticed
that this value was incremented w/o need. It took me a wee while to get to the bottom of it
----
Server IP: 69.147.83.197
Probable Submitter: 81.65.73.5
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/105130
Del: integrated -- https://master.php.net/note/delete/105130/integrated
Del: useless -- https://master.php.net/note/delete/105130/useless
Del: bad code -- https://master.php.net/note/delete/105130/bad+code
Del: spam -- https://master.php.net/note/delete/105130/spam
Del: non-english -- https://master.php.net/note/delete/105130/non-english
Del: in docs -- https://master.php.net/note/delete/105130/in+docs
Del: other reasons-- https://master.php.net/note/delete/105130
Reject -- https://master.php.net/note/reject/105130
Search -- https://master.php.net/manage/user-notes.php