Why compile a PHP File?
Asked Answered
E

5

12

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" programmer, 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?

Expiate answered 5/5, 2011 at 15:9 Comment(3)
I like the user name! Just wanted to throw that out there - As far as your question goes, the guys below pretty much nailed the general consensusHighborn
Unless you are operating at facebook-type scales, there is no point in compiling your php. The performance increase will be insignificant.Copalite
But I do have a question though. If you compile PHP is it decompilable? If not, then selling compiled PHP scripts to Companies seems like a good deal, since they can no longer copy it. :DKorikorie
C
12

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:

Cluj answered 5/5, 2011 at 15:16 Comment(1)
I know this is very old, but I thought I'd note that PHC let the above URL lapse, and it has been picked up by some randoms. The place to find it is here: github.com/pbiggar/phc (It's also no longer maintained)Widower
C
6

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 Alternative PHP Cache (APC) on your machine which will cache the compiled bytecode and should give you an instant performance boost.

Collings answered 5/5, 2011 at 15:10 Comment(0)
D
2

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

Deppy answered 5/5, 2011 at 15:12 Comment(1)
Facebook also has millions of users visit per day.Mccorkle
S
2

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).

Stylist answered 5/5, 2011 at 15:12 Comment(1)
This is quite misleading--PHP code is bytecode compiled and then executed by the Zend engine. It's not executed line-by-line.Brennabrennan
O
1

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.

Oxyacid answered 5/5, 2011 at 15:12 Comment(1)
I am not sure about this being right. PHP creates a bytecode intermediary when the file has changed and. The code stays "Precompiled" and it is only compiled again if the php script has changed and it is the first time it's being demanded before it's precompilation. Therefore it doesn't compile the whole code at once.that would be quite ridiculous and a huge waste of resources.Beckham

© 2022 - 2024 — McMap. All rights reserved.