Page 1 of 1

str_replace

Posted: Sun Oct 05, 2008 12:56 am
by raysleith
btw, which one is faster when using str_replace

Code: Select all

 
$test = array("1"=> "one", "2" => "two");
$string = "1 2";
str_replace(array_keys($test), array_values($test), $string);
 
or

Code: Select all

 
$test = array("1"=> "one", "2" => "two");
$string = "1 2";
foreach ($test  as $key => $value) {
 str_replace($key , $value, $string);
}
 
thanks :D

Re: str_replace

Posted: Sun Oct 05, 2008 2:58 am
by josh
Which one is less code? WHich one is easier to read? WHich one seems like it'd be doing more stuff under the hood of PHP?

FYI foreach operates on a copy of an array ( which means the object is duplicated in memory ). Decide for yourself which way to do it based off this

I dont think performance is really a concern, including files and querying the database are going to be your bottlenecks most of the time