Introducing some programming languages—PHP language

Introducing some programming languages—PHP language

PHP language

Introduction

PHP (PHP: Hypertext Preprocessor), or "Hypertext Preprocessor", is a scripting language executed on the server side, especially suitable for Web development and can be embedded in HTML. PHP grammar learned C language, absorbed the characteristics of multiple languages ​​​​of Java and Perl to develop its own characteristic grammar, and continued to improve and improve itself according to their strengths , such as Java's object-oriented programming. The main goal of this language was to make Developers write high-quality web sites quickly. PHP supports both object-oriented and process-oriented development, and is very flexible in use.

development path

PHP is an open source software project with many developers . It was originally an acronym for Personal Home Page and has been officially renamed "PHP: Hypertext Preprocessor". PHP was in 1994 1994Created by Rasmus Lerdorf in 1994, it was originally just a simple program written in Perl to count the number of visitors to his own website . Later rewritten in C, with database access,1995 1995In 1995 , PHP (Personal Home Page Tools) released the first version of PHP1. Since then, more and more websites have begun to use PHP, and there is a strong demand for some features, such as loop statements and array variables, etc.,1995 1995PHP2 released in 1995 added support for mySQL.

When Andi Gutmans and Zeev Suraski were developing an e-commerce program for a university project, they found that PHP2 was clearly insufficient, so they rewrote the code and released PHP3. PHP3 is the first version similar to the syntax structure of modern PHP . The most powerful function of PHP3 is its scalability. The new functions of PHP3 and the support of a wide range of third-party databases and APIs make it possible to write such programs.

Shortly after the official release of PHP3, Andi Gutmans and Zeev Suraski started rewriting the PHP code. The design goal is to enhance the runtime performance of complex programs and the modularity of PHP's own code. After unremitting efforts, the Zend engine was successfully developed and the design goal was achieved, and in 1999 1999PHP was introduced in mid- 1999 . Based on this engine and incorporating more new features, PHP4 was launched in2000 2000May5, 2000Official release in May . In addition to higher performance,PHP4 also includes some key features, such as: support for more web servers, HTTP Sessions support, output buffering, safer user input, and some new language constructs.

PHP5 in 2004 2004July 7, 2004Officially released in July , its core is the Zend Engine2 2The 2nd generation (PHP7 is the 3rd generation of Zend's enhanced version), introduceda new object model and a large number of new features, andbegan to support object-oriented programming. After a long period of aborted development for PHP6, PHP5 was released6 6The six versions tenaciously supported the development of the open source community until the release of PHP 7.0 on December 3, 2015. In fact, PHP5.6 already contained many features that PHP6 wanted to achieve, and it won valuable resources for the development of PHP7. time. Live up to expectations PHP7.0 compared with PHP5.6 performance improved by2 22 times, the successful release of PHP7 has brought many core development members back to the PHP community, and PHP8 was released on 2020-11-26. Compared with the php7 series, PHP8 adopts a stricter verification judgment mode for various variable judgments and operations, which is beneficial to the performance optimization of jit in subsequent versions.

As a high-level language, the PHP language is characterized by open source and belongs to the C language system in terms of design system. It allows many beginners who have received higher education to quickly accept and complete the introductory learning. It is easy to use and easy to find a job. Natural fans many. If the amount of data is large and the access pressure is high, technical solutions such as redis, mysql sub-table partition sub-database, elasticsearch search engine, message queue write protection and PHP system distributed cluster deployment can be integrated to alleviate data storage, service access and data retrieval. of great pressure.

Whether it is a large, medium or small project, PHP is a very suitable high-level programming language . Whether it is suitable depends on the mastery of the project development team. But for larger and more complex projects, the most common php-fpm programming model shows its weakness. Aiming at a series of shortcomings exposed by PHP-fpm, the simplest and most reliable solution is to upgrade compatible PHP versions in time. Compared with PHP5.6, PHP7.0.0 has improved performance by 2 2In addition, the Preloading preloading mechanism provided by PHP7.4realizes the resident memory of some programsand obtains a good performance improvement. In PHP8, it also provides efficient JIT (Just-In-Time Just-In-Time Compilation) computing support. In addition, developers with a higher level can turn to the more difficult php-cli programming, which can solve most system performance problems. Both PHP7 and PHP8 support this mode of programming.

After more than 20 years of development, with the rapid development and improvement of php-cli related components, PHP can be applied in TCP/UDP services, high-performance Web, WebSocket services, Internet of Things, real-time communication, games, micro-services and other non- System development in the Web domain.

According to statistics released by W3Techs on December 6, 2019, PHP accounts for 78.9 % 78.9\% of the programming languages ​​used on the server side of WEB websites78.9% . 58.7 % 58.7\%of websites with content management systems58.7% of websites use WordPress (CMS system developed by PHP), which accounts for25.0 % of all websites 25.0\%25.0%

Language Features

open source free

PHP is an open source software project with a large audience and many developers. Linux + Nginx + Mysql + PHP is its classic installation and deployment method. All related software is open source and free, so using PHP can save a lot of genuine licensing fees . However, as an open source software, PHP lacks the support background of large technology companies, and the bad news about it on the Internet is enduring. However, the reality of its continuous iteration and continuous performance enhancement is inspiring. The PHP community uses practical actions Give a strong response to all kinds of doubts.

Fast and efficient

The core of PHP is based on C language, which is good and efficient , and high-performance extension components can be developed in C language; the core of PHP contains more than 1000 1000There are 1000 built-in functions with comprehensive functions and concise out-of-the-box program code; PHP array supports dynamic expansion, supports associative array with numbers, strings or mixed key names, which can greatly improve development efficiency; PHP is a weak type language, the pass rate of program compilation is high, and the development efficiency is faster than other strongly typed languages; PHP is naturally hot-deployed, and the hot-deployment is completed when the code file is overwritten in the php-fpm operating mode; PHP passes 2020After more than 20 years of development, a large number of reference materials can be found on the Internet for reference and study.

performance improvement

The higher the PHP version, the higher its overall performance. According to the official introduction, the performance of PHP7.0.0 is improved by 2 2 compared with PHP5.6.2 times, PHP7.4 is about 30%faster than PHP7.030% , PHP8.0 has about 10%improvement in performance compared to PHP7.410% . PHP 8.0 introduces the JIT compiler feature while adding several new language features such as named parameters, union types, annotations, Constructor Property Promotion, match expressions, nullsafe operators, and improvements to the type system, error handling, and consistency . PHP has its own core development team, maintaining5 55 years to release a major version,1 11 month post2 2The frequency of 2 minor versions, the latest version is PHP8.08. PHP 7.0.0 was released on 2015-12-03, the latest version under this version is PHP 7.4.21 (release date: 2021-07-01), PHP 8.0.0 was released on 2020-11-26, the latest version under this version It is PHP 8.0.8 (release date: 2021-07-01).

Cross-platform

Each platform has a corresponding php interpreter version, which means that the binary code (PHP interpreter) of the target platform is compiled for different platforms. The program developed by php can run on multiple operating systems such as windows, linux, and unix without modification superior.

resident memory

In the php-cli mode, the program can be resident in memory , and various variables and database connections can be stored in the memory for a long time to realize resource reuse. The more common method is to combine the swoolecomponent writing cliframework.

page life cycle

In php-fpm mode, all variables are at the page level. Whether it is a global variable or a static member of a class, it will be cleared after the page is executed. It has low requirements for programmers and takes up very little memory. It is especially suitable for middle - level Development of small systems .

operating mechanism

Common operating modes of PHP are 2 2There are two types, namely php-fpm and php-cli. When PHP chooses to run in php-fpm mode, all variables arepage-level, whether they are global variables or static members of a class, they will be cleared after the page is executed. Running in php-cli mode can realize program resident memory, various variables and database connections can be stored in memory for a long time to realize resource reuse, and performance can be greatly improved. Although php-cli is more complicated to develop, it can obtain Higher performance, higher requirements for developers requires a higher level of development, the more common mode is to combineswoolecomponents to write the cli framework, various variables can be stored in the cross-process high-performance shared memory Table, you can develop support for hot The activated php-cli is reliable for various application systems.

php-fpm became an official component in PHP 5.3.3. It provides a stable and reliable process management service . When the number of processes is insufficient, it can intelligently expand the number. When idle, it can automatically recycle and destroy redundant processes . At the same time, it has fault tolerance for programs It is very powerful and runs very stable, which can meet the development needs of enterprise level. php-fpm friendly completes the user's input and output using HTTP/HTTPS and other TCP/IP Internet protocols. The page-level life cycle releases various resources as soon as they are used up, and there is no problem of memory leaks. php-fpm also provides technical support for some resident memory. For example, the opcache.preload introduced by PHP 7.4 can also realize the resident memory of local PHP classes and functions, but this method is not flexible enough, and it is too tied to the server configuration. .

Because php-cli can realize the resident memory of various resources, it can reuse resources, complete multi-process programming and asynchronous programming more efficiently, and develop application systems with higher load capacity. But compared to the simple programming development of php-fpm, developers need to pay attention to many matters and need to do a lot of additional controller development, otherwise the expected high performance cannot be achieved.

First of all, developers need to implement a reliable process management service to ensure that a new process can be automatically created after the system process encounters various errors and exits. Only in this way can it ensure that there are enough free processes for subsequent service requests to be allocated. Each business code segment must be handled with exceptions so that the process will not exit when it encounters a non-fatal error, because restarting the process means that the program and various resources need to be loaded again. This process consumes a lot of performance, so only the process runs stably. It only makes sense to reside in memory.

Secondly, because of the resident memory, the new program written must restart the service to take effect, which will be unfamiliar to developers who are used to the fpm mode. In addition, developers need to manually release memory, otherwise memory leaks will occur after the system runs for a long time. At the same time, in the cli mode, we cannot directly use $_SERVER, $_POST, $_GET, $_COOKIand $_FILESto perform programming work like in fpm. We need to analyze various Internet communication protocols by ourselves to complete user input and output. To achieve multi-process programming and use multi-core CPU computing, and learn to use pcntl and posix programming, this involves asynchronous programming logic, which is still relatively difficult. A more common mode is swooleto write a cli framework in combination with components.

PHP is an active community development language , with a professional core development team that is constantly iterating. The latest version is PHP 8.0.8, which introduces many new features.

main features

  1. PHP's unique syntax mixes C, Java, Perl, and PHP's own innovative syntax.

  2. PHP can execute dynamic web pages faster than CGI or Perl-in terms of dynamic pages, compared with other programming languages,

PHP embeds programs into HTML documents for execution, and its execution efficiency is much higher than that of CGI that completely generates html tags;

PHP has very powerful functions, and all CGI functions can be realized in PHP.

  1. PHP supports almost all popular databases and operating systems.

  2. The most important thing is that PHP can use C and C++ to expand the program!

simple program

A + B problem

<?php
$input = trim(file_get_contents("php://stdin"));
list($a, $b) = explode(' ', $input);
echo $a + $b;

Hello World

<?php
 
echo "Hello World!\n" ;
 
?>

cookie

<html>
<head>
<meta charset="utf-8">
<title> </title>
</head>
<body>

<?php
if (isset($_COOKIE["user"]))
    echo "欢迎 " . $_COOKIE["user"] . "!<br>";
else
    echo "普通访客!<br>";
?>

</body>
</html>

Guess you like

Origin blog.csdn.net/ZH_qaq/article/details/131406081