Home >Backend Development >PHP Tutorial >Compile PHP programs using ZendEncode_PHP tutorial

Compile PHP programs using ZendEncode_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:38:321195browse

1. How ZendEncode works

Everyone who uses PHP knows that it is a script programming tool. The program written by it must be placed on the web server in the form of source code, so we cannot protect it. own source code. And everyone knows that the execution efficiency of any script program is relatively slow compared with the compiled binary code with the same function. So it would be great if there was a tool that could help us compile programs written in PHP into binary code. This would not only improve the execution efficiency, but also speed up the running speed. If there really was such a tool, it would be a two-for-one thing.

Just in time, the just-released ZendEncode was developed for this purpose. ZendEncode can directly compile scripts into binary code. With ZendEncode, you can compile the PHP program you wrote and distribute it to many users without exposing your source code. The compiled binary code can be transparently read by zend Optimizer. That is to say, the customer can execute the PHP program compiled by ZendEncode as long as he installs Zend Optimizer on his server. The compiled program contains part of the Zend Optimizer code, so the program code is further optimized during the compilation process, which means that the execution efficiency of the script is improved.

In a certain sense, ZendEncode is a "PHP compiler". However, it is not a real compiler. A real compiler will run independently of the original compilation environment. Programs compiled by ZendEncode also need the support of ZendOptimizer, just like compiled java binary codes. , which requires a JVM to run. Therefore, ZendOptimizer can be regarded as a virtual machine for PHP compiled code. In any case, it is just such a thing, they have to be used in conjunction with each other.

The operating systems currently supported by ZendEncode are: Solaris, Linux, FreeBSD and Windows. ZendEncode can be run directly, and php does not have to be installed on your system.


2. Installation of ZendEncode (this article uses the installation in the Linux environment as an example)

Download a software package first! ZendEncode is not free software, and you have to pay to use it , and the price is quite high. Fortunately, zend.com offers a trial package that allows users to try it for free for 30 days. This package is available directly from http://www.zend.com/. Therefore, you must first download the ZendEncode and ZendOptimizer software packages from http://www.zend.com/. Secondly, you need to download an authorization file, that is, license. Since ZendEncode is an authorized product, users need to apply for a license from zend.com. The steps to apply for a card are as follows:

To apply for a trial license, you need to provide zend.com with the ID of the computer you are using, that is, the host ID to be filled in on the application page (actually the host ID on your computer The MAC address of the network card), the method to check the computer ID is as follows: Download a lmutil.z program from zend.com, decompress it to get the program lmutil, run it, a sequence string will be generated according to the hardware characteristics of the system. Fill in these serial numbers into the hostid on the license application page. zend.com will generate a license for the user within 48 hours. Download this license file. The file name is zendEncode.dat. It is the only one that can be used on your computer. .

1. Unzip the ZendEncode software package in the /usr/local/Zend directory. After decompression is completed, there is an additional zendenc file in the directory, which is the "compiler".

2. Copy the license file to the /usr/local/Zend directory

The installation is complete.

3. Installation of ZendOptimizer

After completing the installation of ZendEncode, only half of the task is completed. You need to use the compiled php binary code and install an interpreter-ZendOptimizer. With it support, the compiled php binary can be executed correctly.

Unlike ZendEncode, ZendOptimizer is a free software. Its main function is to speed up the running of php script files. According to Zend.com, with the optimization of ZendOptimizer, the execution efficiency of the program can be increased by 600%. After The author's simple test has indeed improved the execution efficiency a lot.

The steps to install ZendOptimizer are as follows:

1. Unzip the zendOptimizer software package and copy the zendOptimizer.so file to the /usr/local/Zend/lib directory

2. Open the /usr/local/lib/php.ini file and add the following two lines to the file:

zend_optimizer.optimization_level=15

zend_extension="/usr/local/Zend/lib/ ZendOptimizer.so”

3. Restart the Apache server to make the above updates take effect


4. Use of ZendEncode

Okay, all the above preparations are completed , now write a simple php script, use zendEncode to compile it, and see the effect. Okay, first write the simplest script and see if the compiled code can be executed:

#vi test.php



Compile it:

#[root@mail Zend]# ./zendenc test.php testencode.php

Zend Encoder Unlimited (TEST DRIVE) v1.1.0 © Zend Technologies, 1999-2000

Licensed to: xqkred.


Compiling test.php...

Done encoding test.php.

Optimizing... Done.

Saving... Done.

Okay , compiled successfully. However, the size of the compiled program is much larger than before.

Copy testencode.php to the release directory of the web server, type http://localhost/testencode.php on the browser, wow, the compiled code can run successfully! Since we are using it for trial use version of ZendEncode, so a picture will appear at the top of the page, indicating that this is a binary file generated by the ZendEncode trial software package. As shown in the picture below:


For users who have purchased the official version, the above icon will not reappear.

Let’s take a look at its execution efficiency! First, write a small calculation program to roughly estimate: compute.php


$t=time ();

for( $i=0;$i<1000000;$i ) {

if(($i )!=0) {echo $i; echo "," ;}

else { echo "
";}

}

$t1=time();

echo "
?>

When this program is executed, it takes the system time. After completion Then take the system time. The difference between the two values ​​is the time required for the entire program to run. First execute it without compilation, then compile it with ZendEncode and execute it again. Mutual comparison results: without compilation, the average running time is 19 seconds, and the average execution time of the compiled code is 9 seconds. It seems that the execution efficiency has been improved a lot.

5. Note

Due to the rapid development speed of PHP, its version number is also updated very quickly. If you follow the above steps, but your browser display is a mess. Garbled characters, then it means that your php does not match the ZendOptimizer version you are using. Just re-download the corresponding ZendOptimizer to solve the above garbled code problem. In addition, pay attention to the version of the software package you download. In the Linux environment, there are glibc and libc. Redhat 6 and later versions should download glibc type software packages.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735103.htmlTechArticle1. Working principle of ZendEncode Anyone who uses PHP knows that it is a script programming tool written by The program must be placed on the Web server in the form of source code, so we cannot protect...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn