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
raysleith
Forum Newbie
Posts: 10 Joined: Sat Oct 04, 2008 10:26 am
Post
by raysleith » Sun Oct 05, 2008 12:56 am
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
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Sun Oct 05, 2008 2:58 am
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