Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was just wondering what makes PHP work faster. I have a few methods that I always go and do, but that only improves the way I can read it, but how about the interpreter?

Should I include the curly braces when there is only one statement to run?

if(...){
    echo "test";
}
# Or..
if(...)
    echo "test";
=== Which should be used?

I have also found http://beta.phpformatter.com/, and I find the following settings to be good, but are they?

Indentation:

Indentation style: {K&R (One true brace style)}
Indent with: {Tabs}
Starting indentation: [1]
Indentation: [1]

Common:

[x] Remove all comments
[x] Remove empty lines
[x] Align assignments statements nicely
[ ] Put a comment with the condition after if, while, for, foreach, declare and catch statements

Improvement:

[x] Remove lines with just a semicolon (;)
[x] Make normal comments (//) from perl comments (#)
[x] Make long opening tag (<?php) from short one (<?)

Brackets:

[x] Space inside brackets- ( )
[x] Space inside empty brackets- ( )
[x] Space inside block brackets- [ ]
[x] Space inside empty block brackets- [ ]

Tiny var names:

often I go through my code and change $var1 to $a, $var2 to $b and so on. I do include comments at the start of the file to show to me what each letter(s) mean..

Final note:

So am I doing the right thing with the curly braces and the settings?
Are there any great tips that help it run faster?

share|improve this question
6  
Code formatting will not make your code faster. –  Mchl Feb 6 '11 at 8:48
    
@Mchl : What I mean is formatting makes it easer for me to read, does that come at a cost for the interpreter to process it? –  JamesM-SiteGen Feb 6 '11 at 8:51
2  
No it does not. –  Mchl Feb 6 '11 at 8:55
    
And even if it did, it would never have remotely enough impact to justify hurting readability. –  delnan Feb 6 '11 at 11:37
    
btw: With my files, I understand every $A-F0-9, $_0 $_1 .. $_9 $a $a0 $a1 .. $ff, so when if I ever reach 255/ff I will simply go and rewrite the whole file, as I have a max of 255 hex codes to remember I usually just read the hex as what I have it mapped to, Okay this did take a while to get used to though its now just a habit. –  JamesM-SiteGen Feb 7 '11 at 6:38
show 1 more comment

3 Answers

You should always code with legibility in mind. If a certain portion of code has performance issues, you optimize it as necessary. This may likely lead to less legible code. In which case you provide extra explanations in comments.

As far as formatting is concerned, it doesn't have an impact on performance. I avoid those bracketless if statements as they are fragile and somewhat obscure. Thus, making your code potentially easier to break and harder to understand.

share|improve this answer
4  
+1 - I NEVER write if statements (or other conditional statements) without braces or indentation. They make codes vulnerable to mistakes and bugs. –  mauris Feb 6 '11 at 10:34
add comment

Indentation style: {K&R (One true brace style)}

Subjective: choose one and stick to it throught your project

Indent with: {Tabs}

Subjective: 'spaces' vs 'tabs' debat will never end

Starting indentation: [1]

Why not 0?

Indentation: [1]

One tab, but at least two spaces

[x] Remove all comments

Nonsense.

[x] Remove empty lines

Nonsense.

[x] Align assignments statements nicely

As long as 'nicely' is really nice

[ ] Put a comment with the condition after if, while, for, foreach, declare and catch statements

A helpful thing if you end up with too convoluted statements. Refactoring into smaller methods is usally better way to improve readibility.

Improvement:

[x] Remove lines with just a semicolon (;)

Makes sense

[x] Make normal comments (//) from perl comments (#)

Both work.

[x] Make long opening tag (<?php) from short one (<?)

Brackets:

Makes sense.

[x] Space inside brackets- ( )
[x] Space inside empty brackets- ( )
[x] Space inside block brackets- [ ]
[x] Space inside empty block brackets- [ ]

Subjective.

Tiny var names:

often I go through my code and change $var1 to $a, $var2 to $b and so on. I do >include comments at the start of the file to show to me what each letter(s) mean..

Why would you do that? It reduces readibility, and you gain nothing.

share|improve this answer
2  
I'm especially confused by the $var1/$a stuff... both are terrible variable names! –  Dean Harding Feb 6 '11 at 11:59
    
I'm assuming that $var1 is meant to symbolize $aMeaningfulVariableName in this case. –  Mchl Feb 6 '11 at 12:03
    
@Dean : @Mchl Is correct, and $a is meant to be only for minimizing the code. –  JamesM-SiteGen Feb 7 '11 at 6:24
1  
@JamesM-SiteGen: Personally I think it is acceptable in one case only: if($conditionToBreakLoop) continue; or break; –  Mchl Feb 7 '11 at 9:27
1  
That's what I'm saying. The difference is that 'break;' or 'contiunue;' are in fuct jump instructions, so there is no risk of somebody adding another instruction below and wondering why it's not being executed. –  Mchl Feb 8 '11 at 8:29
show 4 more comments

Use formatting that is easy for humans to read. The Speed Optimization resources I've found suggest that compiling PHP will make it faster. Other resources suggest that the interpreter will cache the pages, and the cached pages will be optimized.

What this means is that it's not worth your time to do this manually. First, it makes the code unreadable, and second, the interpreter will do this for you, so all you'll accomplish is making the code unmaintainable.

Here is a Stack Overflow Question, PHP Source Code Whitespace, that suggests there are very minute speed gains. However, I don't think it's worth the tradeoff of readibility for very little gain.

I suggest that you instead focus on growing the product, adding more features, and focusing on your business goals. You'll reap more rewards by focusing on those goals instead of over-optimizing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.