0

How to define multiple empty variables in PHP This style:

$variable1 = '';
$variable2 = '';
$variable3 = '';

Or like that:

$variable1 = $variable2 = $variable 3 = '';

Which method is faster from server point of view?

Cheers

Arm Peev
  • 128
  • 4
  • 5
    If you can actually measure any significant difference between the two, I'll be amazed – Mark Baker Mar 04 '15 at 12:14
  • 2
    And if this is the best performance optimization that you can find to do in your code, then I'll be amazed – Mark Baker Mar 04 '15 at 12:14
  • I wonder if you're writing code for a satalite, or maybe a space probe, or maybe missile systems? NUCLEAR MISSLE SYSTEMS? :P – Afaan Bilal Mar 04 '15 at 12:15
  • 2
    If you really want an answer to this question, I suggest that you implement the Vulcan Logic Dump, and look at the differences in opcodes generated by each of the two statements – Mark Baker Mar 04 '15 at 12:18
  • So unless I'm writing code for space probes or missile systems I'm not allowed to ask questions that came up in my mind. Understood. – Arm Peev Mar 04 '15 at 12:34
  • 1
    Oh come on now, don't be like that. And well, I think @MarkBaker answered it, no? – Afaan Bilal Mar 04 '15 at 12:57
  • The fastest way to define a variable is simpy $var; The ones you are defining, are not empty. They contain a string. – Erik Mar 04 '15 at 13:04
  • @Erik `$var;` does in fact not do anything. http://stackoverflow.com/a/28826024/476 – deceze Mar 04 '15 at 13:20
  • The point is that the best way to find an answer to questions about execution speed is to try it yourself; but that micro-optimizations like this will have negligible impact in most systems.... especially as most systems have much more serious performance bottlenecks – Mark Baker Mar 04 '15 at 13:22
  • I'd also note that the for variables in your example, an array would probably be more appropriate.... if you had `$foo = $bar = $baz = '';`, that would be a slightly different matter – Mark Baker Mar 04 '15 at 13:24

2 Answers2

0

This type of variable defining:

$var1 = '';
$var2 = '';
$var3 = '';
$var4 = '';
$var5 = '';

took 112 milliseconds for 100000 repetitions.

While this :

$var1 = $var2 = $var3 = $var4 = $var5 = '';

took only 60 milliseconds.

Next test, only with two variables:

$var1 = '';
$var2 = '';

vs

$var1 = $var2 = '';

Numbers are 76ms vs 48ms for 100000.

Maybe is not too much, but why not to define variables in optimal way.

Thanks

Arm Peev
  • 128
  • 4
  • Did you execute both tests in the same script, or at least in separate functions to allow for scoping (if so, then they're null and void as you're not doing the same thing because one test (the first) actually needs to create the variables in the first place, the other doesn't) – Mark Baker Mar 04 '15 at 13:09
0

Well, Here I found some already answered questions which may help you:

How can I measure the speed of code written in PHP?

Tracking the script execution time in PHP

http://www.developerfusion.com/code/2058/determine-execution-time-in-php/

Basically, the trick is using microtime(). Have a look at them and see if any of the methods mentioned can measure to that precision as you require.

Hope it helps!

Community
  • 1
  • 1
Afaan Bilal
  • 333
  • 3
  • 15