There are too many criticisms on PHP. Based on my personal experience working with PHP, I would like to some reviews on this programming language. The following values are comparable to other things from the PHP point of view of Web programming and not others. The article will consist of 4 parts, the first part will talk about the design and implementation of PHP.
As mentioned above, I only talk about PHP Web (not counting PHP running as Command Line, actually PHP CLI is a bit different but not too much).
Connecting to Web Server
PHP is not designed to handle requests directly but through Web Server (Usually Apache or Nginx). When the client (Web Browser / HTTP Client) sends the request to the Web Server. The web server will connect to PHP and create an independent process to handle that request. One feature is that those processes do not share resources with each other. This means that two requests sent by two clients create two completely separate processes for processing resources. Separate resources include: RAM, CPU, connection … After the request completed, the results returned to the Web Server and the client, the process ends. Allocated resources (memory, CPU, other I / O connections, etc.) are released.
Connect with PHP Extension
PHP is not designed under virtual machine like JAVA, PHP runs on Zend Engine. Zend Engine translates the PHP code into machine code and executes it. All PHP resource management is undertaken by Zend Engine. Zend Engine itself provides a number of libraries for PHP to be run directly without external libraries, but most of those libraries are Text processing libraries. Other PHP libraries are written as extensions, these libraries mainly work with PHP through Zend Engine. Many of PHP’s I / O processes are via external libraries, not core support. For example: DB connection, working with HTTP, image processing, etc.
Simple, flexible: From the design of PHP, plus the fact that PHP is a Script language and the syntax is quite comfortable, it is very flexible. PHP syntax is also very easy to learn so a lot of people know PHP. For someone who does not know anything about programming, it only takes a few months to start PHP and even start … making money. Because of that ease, the number of developers is very large and aggressive.
Support by the great community: PHP owns one of the largest developer communities. The number of jobs in PHP is always among the top. Because of such a large community, almost any technical problem you encounter can have support immediately. If you have used less popular programming languages, you will see how important the community is.
Very good support for word processing: PHP has a lot of great text-related word processing. PHP is based on Perl, a programming language born to work with Text. PHP is great for solving text-related problems, but HTML or the Web is a word processing problem. Therefore, it is easy to understand why PHP is the most popular language for building websites.
There are many extensions, frameworks, libraries: Along with owning a large community, PHP also owns a multitude of libraries, extensions, and lots of frameworks. Therefore, PHP can solve many different problems. Almost mentioning anything can also have libraries related to PHP. Therefore, people sometimes think of PHP as the solution to all problems.
Despite its strong points, PHP also has deadly weaknesses. Weaknesses make some PHP problems very difficult or even impossible to solve.
Do not share resources: The first problem that I also consider is the biggest limitation of PHP is not sharing resources between processes. The process framing is of great help for PHP without having to deal with issues like memory management (PHP Developer doesn’t care much about this), system crashes (one or two dead processes). It has no effect on the system). However, this has led to many other limitations.
The first is that not sharing resources makes resources usage (RAM, CPU, I / O connection) increase very fast. Imagine having 100 requests processed at the same time, equivalent to 100 processes being run. If the same variable is used, the amount of RAM used will be 100 times (other languages use the same RAM so the memory cache is very simple), the number of DB connections must be 100 connections at the same time. (in the next section I will analyze more examples of this connection) … This is completely different from languages like JAVA, .NET, Node JS …. Therefore, when using PHP for a large system, it is extremely difficult to scale the system, the threshold of PHP.
The second is it seem to be too flexible: This is raised as a strong point of PHP, but also its deadly point. PHP is so easy, so flexible, that makes Developer countless ways to get results. In addition, it is so easy to learn that the quality of PHP developers is much lower than most other programming languages. Poor expertise meets a language that is too flexible, the usual outcome is that the code quality is poor, too many Technical Debt are generated. This makes maintaining a PHP project so horrible (not to mention the backwards compatibility of frameworks and some external factors). PHP also has too many external libraries, sometimes the quality is bad, making the situation worse.
The third problem is that it depends too much on the extension: The support processing from PHP’s Core is very limited so PHP has to rely heavily on external Extension libraries. External extensions do not interact directly, but work with PHP through Zend Engine. This mechanism also makes things slower. So sometimes extensions do not help improve too much. I take a typical example that is very often connected to Database. While other programming languages often use Connection Pool to manage connection with DB. PHP uses another mechanism called persistent Connection to accelerate the connection.
Persistent Connection helps when a request is completed, the DB connection created by that Request is not “closed” but continues to be used at the next request. This helps reduce connection time with the DB. However, when the Request has not stopped, this connection cannot be shared with another request (even at the present time, that request does not manipulate the DB). This leads to if PHP must handle 100 concurrent requests, there should be 100 connections to the Database (if increased to 1000, that number is 1000). This is completely different from the connection pool mechanism of languages like JAVA. The Connection Pool ensures that only a maximum of x Connection to a DB is created (x set by the Developer). Even if there are 100 requests, or 1000 concurrent requests to the Web Server at that time, the number of x does not change (If there are more than x requests that need to connect at the same time, there will be a number of requests to wait, please note here. It can have 1000 concurrent requests but at the same time there can be only 100 DB connections, and 900 others are processing some other data). This helps JAVA control the number of connections to the DB, thereby ensuring the performance of the DB, while PHP is not possible.
After receiving feedback from many of my friends, I would like to update to remove the evaluation related to Non-Blocking I / O. In fact, PHP has many options to support this part already. The Performance part of the language is actually not too big a problem, mainly related to big data processing problems or images and videos.
Finally, it does not support Non-blocking I / O, Performance is poor: One of the other weaknesses of PHP is that it does not support Non-blocking I / O. This means that if a process needs to call in I / O, that process will “wait” until I / O processing is done. During that waiting process all the resources of that process remain occupied (especially the CPU). A PHP process takes significantly less resources than other languages (for example, JAVA Web). But 1,000 processes running at the same time cost a lot more than other guys. Another point to mention is that the processing functions in the core of PHP also run much slower when compared to other programming languages. If compared with JAVA, if the large loop can see PHP a hundred times worse, compared with C ++, even a thousand times worse.
When to use PHP and when not to
PHP is suitable for projects:
- Not too complicated in terms of computational processing
- The number of visits is small or medium, or the number of visits is large, but the logic is not too complicated.
- Especially suitable for problems related to the Web interface
- Developer quality is at average or higher (Dev quality is poor, then difficult to work)
PHP is not suitable for developing projects such as:
- Requires real-time, extremely many connections with extremely fast processing time
- Handling very large number of requests with very complex logic.
- Need to handle big Data, Big Data problems.
Although PHP is not designed to solve extremely complex problems, it is actually still good for handling large amounts of requests and is still a powerful programming language. In addition to PHP, it is good for web-based problems in general, especially for sales or management related problems.