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

Variable usage

Postby anon404 » Sun Feb 07, 2010 11:34 pm

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:
Syntax: [ Download ] [ Hide ]
$var = "abc";
do_something_abc($var);
$var = "def";
do_something_def($var);
 


2:
Syntax: [ Download ] [ Hide ]
 
$var_a = "abc";
$var_b = "def";
do_something_abc($var_a);
do_something_def($var_b);
 
anon404
Forum Newbie
 
Posts: 12
Joined: Wed Dec 23, 2009 5:09 am

Re: Variable usage

Postby limitdesigns » Mon Feb 08, 2010 3:09 am

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.
limitdesigns
Forum Commoner
 
Posts: 25
Joined: Sat Feb 06, 2010 10:05 pm

Re: Variable usage

Postby josh » Mon Feb 08, 2010 2:33 pm

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!
Josh - Devnet's angriest programmer.
User avatar
josh
DevNet Master
 
Posts: 4255
Joined: Wed Feb 11, 2004 4:23 pm
Location: Palm beach, Florida

Re: Variable usage

Postby Darhazer » Mon Feb 15, 2010 6:58 pm

Actually
Syntax: [ Download ] [ Hide ]
$var = 'abc';

is faster, since PHP doesn't need to parse the string and replace variables in it :-)
Brave new world... opening gates... 10 Aug 2010
Image
Image
User avatar
Darhazer
Forum Regular
 
Posts: 682
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria


Return to PHP - Theory and Design

Who is online

Users browsing this forum: Jonah Bron and 1 guest