tags:

views:

238

answers:

5

I am familiar with the concept of minification in Javascript and how whitespaces contribute to JS file sizes, but does that concept not apply to PHP scripts?

does whitespace make php scripts file sizes larger? can we minify php scripts? or is there no point in doing so because whitespaces in source codes in php does not affect anything at all?

+4  A: 

There's no point in doing so because users don't download your PHP file. There's no need to minify server-side scripts.

danielkza
That being said, you don't want a script of 1 MiB size to be included in every page request. That *does* have a performance hit. But yes, "minification" won't actually help there :-)
Joey
I don't know if it's really a big performance up but `minification` might improve the performance of the parsing. But if you go that way that's better to use bytecode cache utils like `APC`
RageZ
+1  A: 

If your php program contains html code also, you may want it to minify that, but i would not recommend that, as now you have to keep two copies of the same file, one for development, so that you can understand and one for server. Its better to improve speed using other alternatives and if absolutely required, use this way.

Priyank Bolia
The recommended approach for minifying HTML output would be output buffering combined with an automated minification step and possible caching. IMHO that's rarely worth the trouble though, as all you can do is strip out a bit of whitespace, which hopefully is a negligible portion of the source anyway.
deceze
Actually, just gzipping the stuff is mostly more efficient.
Boldewyn
Does that require dedicated servers, as simple php hosting sites won't have such things. I don't recommend the output buffering method nor the stripping of whitespaces from the php source files, as mentioned above.
Priyank Bolia
+2  A: 

There is a small gain in speed, if you have optimized whitespace in PHP files (reading in and compiling the file), but it is, compared to the rest, insignificant (that said, you can gain more rewriting all unnecessary 'preg_replace' with the appropriate string methods).

Actually, there are possibilities to precompile a PHP file and present this optimized code to the PHP interpreter. Search for Zend Encoder or IonCube. But then, the source file is no more human-readable.

For JS files the gain in speed stems almost to 100% from the smaller loading time via HTTP.

Boldewyn
A: 

Save the following content in the file test.php

<?php
    //Hello I'm comment
    echo "Some code";
                              # There are spaces on the beginning of the line


    if (isset($_GET['a'])){
      var_dump($_GET['a']);
    } else {
      /*
        Some other comment
      */
      die('oooops');
    }

Run $ php -w test.php And it will display source with stripped comments and whitespace

<?php
echo "Some code"; if (isset($_GET['a'])){ var_dump($_GET['a']); } else { die('oooops'); }

Anyway you should use bytecode cache libs to speed up your code. Removing of whitespace is inefficient approach.

Vedeney
+1  A: 

There's no real performance benefit from removing whitespace from PHP scripts. The interpreter simply ignores it.

If you really want to remove it, running

php -w foo.php > foo_small.php

will remove whitespace and comments.

What will have an effect on performance for the end-user (assuming web environment) is the size of the output. The normal thing is to use compression - look into setting zlib.output_compression=TRUE or alternatively ob_gzhandler.

Pete