TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

What PHP chunk of code is FASTER?

We all know that xHTML/CSS and JS minification and compression benefits large traffic sites.

Take a look at the following code:

include("template.funcs.php");
include("country-redirect.php");
if(($country=='PL')||($country=='POL'))
{
    header('Location: index_pol.php');
    exit();
}
$include_path = './global_php/';
$page = "couture";
include $include_path."shop.inc.php";
$movie_num = 1 ;

Now see the minifed version:

include("template.funcs.php");include("country-redirect.php");
if(($country=='PL')||($country=='POL')){header('Location: index_pol.php');
exit();}
$include_path='./global_php/';$page="couture";include $include_path."shop.inc.php";
$movie_num=1;

Which one do you think is faster - in general, I want to start also minifying my programming too, small var and string names like $a rather than $apple and trying to remove as much extra character as possible. Will the PHP Compiler like a compressed chunk or spaced out one?

Upvotes: 2

Views: 373

Answers (7)

Tony Arnold
Tony Arnold

Reputation: 641

Well, when the PHP engine parser does its magic on your code it automatically removes all white space and comments any ways. The difference would be maybe 1/10 of a second if you start getting up in the megabytes worth of text. But this is simply the webserver parsing the files.

If you want real over head ideas search Google for "Best PHP Practices" and get into good habits. I can see from your above snippet you might need some. Just some advice.

Upvotes: 1

jncraton
jncraton

Reputation: 9132

As others have said, minification of PHP code will make no real difference in execution speed. That is the answer to your question, but I think that this quote is also relevant:

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Please don't minfiy the primary version of your code. Please don't use variable names like $a instead of $apple. The ability to read and understand your code is far more valuable than any space savings or marginal speed increase that you may get from minification.

Upvotes: 1

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41895

The minified version won't be faster for 2 reasons:

  • The script is not sent to the client, but interpreted, and then the result is sent
  • Minifying a php script has very little impact on performance

If you want to seed up your php code, you can install a php accelerator

Upvotes: 1

genesis
genesis

Reputation: 50976

This really doesn't matter, as it's not transfered by internet to the client.

Upvotes: 0

Brad
Brad

Reputation: 163262

PHP doesn't care if your code is minified or not.

Write code so you can edit it cleanly later. Minification has no measurable impact on performance.

The reason that you see minified CSS/JavaScript is not for parsing/execution speed... it is for cutting down the file size for data transfer. Your PHP is processed server-side. The only thing that is sent is the output of your code.

Upvotes: 4

Mat
Mat

Reputation: 206709

PHP code stays on the server, so it's size is essentially irrelevant.

Removing those newlines is a really bad idea. Make your code readable for humans.

Upvotes: 5

Pekka
Pekka

Reputation: 449475

Will the PHP Compiler like a compressed chunk or spaced out one?

It does not matter. Any difference between these will be in the microseconds at best.

Making code readable is the only thing that matters.

Upvotes: 10

Related Questions