Page 1 of 1

Variable usage

Posted: Sun Feb 07, 2010 10:34 pm
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);
 

Re: Variable usage

Posted: Mon Feb 08, 2010 2:09 am
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.

Re: Variable usage

Posted: Mon Feb 08, 2010 1:33 pm
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!

Re: Variable usage

Posted: Mon Feb 15, 2010 5:58 pm
by Darhazer
Actually

Code: Select all

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