Home > Article > Backend Development > Use Zend Encode to write and develop PHP programs_PHP tutorial
Anyone who uses PHP knows that Zend Encode is a script programming tool. Programs written with it must be placed on the Web server in the form of source code, so we cannot protect our source code. Everyone knows that the execution efficiency of any script program is relatively low compared with 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 was such a tool, it would kill two birds with one stone.
This is no longer a dream, Zend Encode was developed for this, it can directly compile scripts into binary code. With Zend Encode, 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 Zend Encode 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, Zend Encode is a "PHP compiler".However, it is not a compiler in the true sense, because the truly compiled program can be run without the original compilation environment, and the program compiled by Zend Encode requires the support of Zend Optimizer. Just like compiled Java binary code, it requires JVM support. Therefore, Zend Optimizer can be regarded as a virtual machine for PHP compiled code. In any case, they are used in conjunction with each other.
The operating systems currently supported by Zend Encode are: Solaris, Linux, FreeBSD and Windows. Zend Encode can be run directly, and PHP does not have to be installed on the computer system.
Installation of Zend Encode
Download a software package first! Zend Encode is not free software, 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, first go to www.zend.com to download the Zend Encode and Zend Optimizer software packages. Secondly, you need to download an authorization file license. Since Zend Encode is an authorized product, users need to apply for a license from zend.com. The application steps 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, fill in the host ID (actually the MAC address of the network card on your computer) on the application page. . 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, it will generate a sequence string according to the hardware characteristics of the system. Fill in this serial number into the host ID 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 can only be used on this computer. .
1. Unzip the Zend Encode software package to 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 and the installation is completed.
Installation of Zend Optimizer
After completing the installation of Zend Encode, only half of the task is completed. To use the compiled PHP binary code, you also need to install an interpreter - Zend Optimizer. With its support, compile The resulting PHP binary can be executed correctly.
Unlike Zend Encode, Zend Optimizer is a free software whose main function is to speed up the running of PHP script files. According to Zend.com, with the optimization of Zend Optimizer, the execution efficiency of the program can be increased by 600%. After the author's simple test, the execution efficiency has indeed improved a lot.
The steps to install Zend Optimizer are as follows:
1. Unzip the Zend Optimizer 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.
Using Zend Encode
Now that all preparations are completed, let’s write a simple PHP script and compile it with Zend Encode to see the effect. First write the simplest script and see if the compiled code can be executed:
#vi test.PHP
<? Phpinfo(); ?>
Compile it:
#[root@mail Zend]# ./zendenc test.PHP testencode.PHP
Zend Encoder Unlimited (TEST DRIVE) v1.1.0 (c) 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 a trial version of Zend Encode, a picture will appear at the top of the page, indicating that this is a binary file generated by the Zend Encode trial software package. In the official version of the software, pictures will not be reproduced.
Let’s take a look at its execution efficiency below! First write a small calculation program to roughly estimate:
[compute.PHP]
<?
$t=time();
for( $i=0;$i<1000000;$i++) {
if(($i%20) !=0) {echo $i; echo ",";}
else { echo "<br>";}
}
$t1=time();
echo "<br> "; echo "It used:"; echo $t1-$t; echo " seconds";
?>
When this program is executed, it takes the system time, and after completion, it takes the system time, two values The difference is the time required for the entire program to run. First execute it without compilation, then compile it with Zend Encode and execute it again. 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. (Source: Fengshan Online Academy)