Skip to main content

Why compile a PHP File?


a friend of mine told me recently "to optimize your site you may want compile your php files" and i was like "what?"



i honestly i never heard of that, i'm a "advanced-naive" prgrammer, that means i'm self taught, i built complex sites but i'm still missing something...



bottom line: what does it mean compile php? convert them in exe files? why? is faster?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. While PHP code needs to be interpreted on every call, bytecode is precompiled code that runs almost instantly.
    Mostly you will only really need it, if you are running a larger website.

    The following tools can be used to compile scripts or run compiled scripts:


    eAccelerator
    Zend Optimizer
    PHC (http://www.phpcompiler.org/)
    Alternative PHP Cache (APC)

    ReplyDelete
  2. Unless you're after serious performance then compiling PHP using something like Facebook's HipHop is probably a bit excessive.

    I'd just install/configure APC on your machine which will cache the compiled bytecode and should give you an instant performance boost.

    ReplyDelete
  3. Facebook use such things. Their product is hiphop, and it's free.

    ReplyDelete
  4. The idea of a compiler, is to convert human readble code (C, PHP, Java etc), into machine readable code. When you execute your PHP scripts, they are interpreted (almost inline compilation), which means they are read a line by line, and the code is executed accordingly.

    Compiled code, means that it is compiled at source, therefore is already in machine language (or byte code for VM languages like Java), and therefore, the server does not have to interpret the code each time. This makes it quicker.

    Facebook created a PHP compiler to speed up their site. The idea of compiled code is that usually, once it is written, it doesn't change for a while, so there is an overhead in having to interpret it into machine language each time the code is executed. That is why your friend means by optimize.

    It will therefore be converted into machine language or bytecode (not exe, but effecively the same concept).

    ReplyDelete
  5. The bigger your application the more sense this makes. PHP loads your whole program into memory and then compiles it on the fly: meaning as it needs to be used. So if you pre-compile it should skip that step. Facebook does something like this. The translate their php into C++ via something called hip hop. Not exactly the same thing but you get the idea.

    I doubt this will show you much difference on smaller applications.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.