str_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raysleith
Forum Newbie
Posts: 10
Joined: Sat Oct 04, 2008 10:26 am

str_replace

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: str_replace

Post 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
Post Reply