PHP
PHP (Hypertext Preprocessor) is a general purpose open source scripting language, its syntax absorbs the characteristics of C language, Java and Perl, easy to learn, widely used, mainly suitable for Web development.
What is PHP?
PHP (full name: PHP: Hypertext Preprocessor, "PHP: Hypertext preprocessor ") is a general-purpose open source scripting language. The PHP script is executed on the server. PHP is free to download and use. lamp PHP is easy for beginners to learn.
PHP's unique syntax is a mixture of C, Java, Perl, and PHP's own syntax, and it can execute dynamic web pages faster than CGI or Perl.
Compared with other programming languages, PHP is a program embedded in HTML (an application under the Standard General Purpose Markup Language) document to execute, the execution efficiency is much higher than the full generation of HTML markup CGI.
PHP can also execute compiled code, which encrypts and optimizes the code to run faster.
With the official release of PHP 7Alpha 1 in June 2015 and the GA release on December 3 of the same year, the release of PHP 7 was a milestone for PHP. In terms of performance, PHP 7 is about twice as efficient as the original PHP 5, and is comparable to HHVM.
Compared to PHP 5.6.x, PHP 7 has the following major new features:
Improved performance: PHP 7 is twice as fast as PHP 5.6 64-bit support Many critical errors can be handled with exceptions Remove old and unsupported SAPIs and extensions null merge operator (? ?) Associative comparison operator (<=>) Scalar type declaration Anonymous class
Of course, there are many differences between PHP 7 and previous versions, but most of them are compatible with previous versions, so in most cases you can migrate to PHP 7 without changing the code.
We first introduce the advantages of the PHP language, operating mechanisms and principles, as well as the new features of PHP 7, to help readers have a preliminary understanding of PHP.
Advantage of PHP language
PHP has the following advantages:
- PHP is quick to learn, low development cost, relatively simple syntax, and provides a wealth of class libraries, such as GD library for image processing, various encryption extensions (such as OpenSSL and Mcrypt, etc.), can be easily used directly.
Many libraries come with the PHP environment by default.
- PHP combined with Linux, Nginx or Apache, MySQL can easily and quickly build a set of systems, PHP also supports direct call system commands, so that you can use code to complete a lot of Linux operations. Such as packaging compression, copy and paste, rename, perform grep query in Linux and so on.
Nginx is a very excellent Web server software, Nginx can receive client requests, send PHP files to PHP programs to execute, PHP in Nginx uses the form of fastCGI to run scripts.
- PHP supports MySQL, MSSQL, SQLite and other databases, among which the combination of MySQL is the most popular.
PHP provides three extensions to connect to MySQL, including the MySQL extension, MySQLi extension, and PDO extension, among which:
The MySQL extension is no longer supported in PHP 5.5 and later; MySQLi is a more secure and efficient extension launched by PHP specifically for linking MySQL, and provides more advanced operations, fully support object-oriented; PDO extension is a unified solution for linking MySQL and other types of databases launched by PHP. It is highly portable and can be used to switch between different types of databases flexibly and conveniently without changing more code.
PHP is the interpretation of the execution of the script language, after writing the program can be executed immediately, unlike C, Java, C++ and other languages need to compile and then execute, which makes the development of PHP more efficient.
The configuration files used in php are relatively simple, and the configuration files related to PHP operation are commonly used in php-fpm.conf and php.ini, and the configuration parameters are also easy to understand.
Changes to PHP's configuration file do not need to be restarted to continue running, because PHP is actively added to the configuration file before running the program, which is much more convenient than other languages such as Java.
- PHP, as the most popular and widely used Web development language, has a rich ecosystem with many well-known open source frameworks available, such as:
Official Zend Framework, CakePHP, Yaf, Symfony, etc.; Open source forums have Discuz! , PHPwind, etc.; Open source blog WordPress; Open source online store systems such as Ecshop, ShopEx, etc.; Open source SNS systems such as UCHome, ThinkSNS, etc.
Based on these excellent open source systems, you can easily and quickly set up a Web site. In addition, the active community atmosphere can also help you quickly solve problems encountered during development.
Combined with LVS load balancing, message queue, database master and slave technology, PHP can support the application of general large-scale websites, meet the application development in most scenarios.
PHP itself is developed by C language, in some cases with strict performance requirements, you can also use C language to write PHP extensions to improve the execution speed of the program, the use of PHP to complete the main business code writing, the use of C to complete the performance improvement needs, which makes it possible to ensure the efficiency of software development while taking into account the efficiency of execution.
In the case of this extreme pursuit of software development speed and program execution performance, if it is other languages, it may make you helpless, or overturn.
- Many large domestic companies, such as Baidu, Taobao, 360 and other companies have widely used PHP as a development language, and have achieved great success in specific practice, and there are many successful experiences for reference.
PHP operation mechanism and principle
PHP consists of a kernel Zend engine and an extension layer, where:
The PHP kernel is responsible for handling requests, completing file stream error handling and other operations, and the Zend engine can convert PHP program files into machine language that can be run on virtual machines. The extension layer provides some function class libraries required for application layer operations, such as array and MySQL database operations.
The Zend engine is implemented in C language, parsing PHP code into executable opcode through lexical syntax and realizing corresponding processing methods, basic data structure memory allocation and management, etc., and providing corresponding API methods for call.
The Zend engine is the core of PHP and all the peripheral functionality is implemented around it. The extension layer provides various basic services and built-in functions in the way of components, and the standard library is implemented through it. Users can also write their own extensions to implement specific requirements.
Server Application Programming Interface (SAPI), a series of hook functions that allow PHP to interact with peripheral data. The PHP program we usually write is to get different application modes through different SAPI methods, such as Web applications achieved by WebServer and scripts running on the command line.
When a PHP program is executed, it will be parsed into opcode instructions, and then executed in the virtual machine in order, because PHP itself is developed in C language, so it calls C functions at the time of execution. opcode is the most basic unit of execution of PHP programs.
HashTable is the core data structure of Zend, implementing almost all the functions in PHP, supporting key->value queries, adding and deleting complexity is O(1), supporting linear traversal and mixed types.
HashTable has both a hash structure in the form of key->value and a bidirectional linked list pattern, making it very convenient to support fast lookups and linear traversals.
The hash structure of Zend is a typical hash table model, and conflicts are resolved through a linked list.
Zend's HashTable is a self-growing data structure that, when the number of hash tables is full, dynamically expands itself by a factor of two and rearranges the element positions, all with an initial size of 8.
In addition, when performing the key->value quick lookup, Zend itself also made some optimization, through the way of space for time to speed up. For example, a variable called nKeyLength is used in each element to identify the length of the key for quick determination.
Zend HashTable implements linear traversal of elements through a linked list structure. In theory, it is enough to use a unidirectional linked list to do traversal, and the main purpose of using a bidirectional linked list is to quickly delete the linked list elements and avoid traversal.
PHP is a weakly typed language that does not strictly distinguish between the types of variables. PHP does not need to specify a type when declaring a variable. PHP may implicitly convert variable types during program execution. As with other strongly typed languages, explicit type conversions can be performed in programs.
Zval is another very important data structure in Zend that identifies and implements PHP variables.
Zval mainly consists of the following three parts:
Type: Specifies the type of the variable (integer, string, array, etc.); refcount&is_ref: used to implement reference counting; value: The core part that stores the actual data of the variable.
Zval is used to hold the actual data of a variable. Because there are multiple types to store, zval is a union, and thus weak typing is implemented.
Reference counting is widely used in memory reclamation, string manipulation, and so on. Variables in PHP are a typical use of reference counting. Zval's reference count is implemented through the member variables is_ref and ref_count. With reference counting, multiple variables can share the same data, avoiding the heavy consumption of frequent replication.
On assignment, Zend points the variable to the same Zval, with ref_count++, and on unset, with ref_count-1. The actual destruction operation is performed only when ref_count is 0. If the assignment is by reference, Zend will change is_ref to 1.
PHP variables share data through reference counting. When trying to write a variable, if Zend finds that the Zval pointed to by the variable is shared by multiple variables, it copies a Zval with ref_count 1 and decrements the refcount of the original Zval. This process is called "Zval separation".
It can be seen that Zend only performs a copy operation when a write operation occurs, so it is also called copy-on-write (copy on write).
For referenced variables, the requirement is the opposite of non-referenced variables, the variables assigned by reference must be bound to each other, and modifying one variable modifies all the bound variables.