Variable usage

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
anon404
Forum Newbie
Posts: 12
Joined: Wed Dec 23, 2009 4:09 am

Variable usage

Post by anon404 »

Is there any efficieny difference between these 2 examples. I have been using method 1 but would rather use the second. If there is no difference what would be recommended?
1:

Code: Select all

$var = "abc";
do_something_abc($var);
$var = "def";
do_something_def($var);
 
2:

Code: Select all

 
$var_a = "abc";
$var_b = "def";
do_something_abc($var_a);
do_something_def($var_b);
 
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Variable usage

Post by limitdesigns »

If there is any difference at all, it wouldn't be noticeable. Slow-downs that are results of trivial things like this are only apparent when you have a huge application, so I wouldn't worry about it.

As far as which one is recommended, the second option looks like it's more organized, but it's hard to say, without knowing what exactly you're doing with this code.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Variable usage

Post by josh »

Answer: neither, most efficient (and also readable) would be use no variable at all.

But yeah the real answer is it doesn't matter. Don't worry about saving 1ms of variable time when you are going to have 500ms database queries later on!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Variable usage

Post by Darhazer »

Actually

Code: Select all

$var = 'abc';
is faster, since PHP doesn't need to parse the string and replace variables in it :-)
Post Reply