The PHP Programming Language
What could be the reasons 83% of the 10 million most popular websites of the world use PHP as their server-side technology? Let's see together
Introduction
PHP is a computer programing language first appeared on the scene 29 years ago (1995). It is a mainly dynamically, weakly typed, object-oriented, reflective, imperative, functional and procedural programming language with automatic memory management whose main purpose is to be run by web servers to generate dynamic web pages and images for websites.
The language invented by Rasmus Lerdorf was initially conceived as a simple set of routines and tools to manage the personal web pages of the developer. The author published the first release of the software gaining an immediate favour from the community. As per the words of the same author: "there was never any intent to write a programming language […] I have absolutely no idea how to write a programming language ...". Still the adoption of PHP grew so much and in such a short time that features and functionalities were quickly added one after the other without any design process at the base of the development of the language. This has led during the years and the various versions of the language to a serie of inconsistencies and abandoned developments, like the never released version 6 which tried to make PHP Unicode compliant, without however having these missteps ever mined the huge popularity of the language.
Today PHP is an evolved, full-fledged programming language used by 83% of the 10 million most popular websites of the world, with the notable accomplishment of having been the language originally used by Mark Zuckerberg to write the first versions of the whole Facebook platform.
First glance at the code
Example: The classic "Hello world" program written in PHP:
Main features of PHP
Technical features
Mainly dynamically, weakly typed
PHP has an hybrid type system which is mainly dynamic because variables have no type, and mainly weak because scalar types are automatically converted between them or strings. At the same time, with PHP 7 these automatic conversions can be disabled, and since versions 5.x type declarations are supported and enforced at runtime for function parameters and, in version 7, even for function return types. PHP has the following built-in data types:
Name | Description |
boolean | trUe/fAlse (TRUE or FALSE are case-insensitive) |
integer | a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...} |
float | floating point numbers (also known as "floats", "doubles", or "real numbers") |
string | a sequence of 1 byte characters, PHP only supports a 256-character set, does not offer native Unicode support |
array | arrays in PHP are actually ordered maps that associate values to keys. They can be list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue... |
object | see below Object-oriented |
callable | functions or object methods, including static class methods |
resource | variable holding a reference to an external resource (Ex: database) |
NULL | NULL is the only possible value of type null |
Object-oriented
As with other PHP's characteristics, the object-oriented features of the language have widely changed during the years. For example, in previous versions objects where passed to functions by value. Today PHP is a complete class-based object-oriented language with classes and interfaces, and supports both abstract and final classes and methods. A class cannot derive from a primitive type and multiple inheritance is not allowed but the language supports Traits.
Memory management
Memory management in PHP is automatic. As of 5.3.0 PHP implements a synchronous concurrent cycle collection algorithm in reference counted systems garbage collector to free the memory resources that become not needed by the program anymore.
Security
PHP is very popular, and being used on the vast majority of websites of the world, systems who use it are obviously very often target of hackers attacks. For example 30% of all the vulnerabilities listed since 1996 in the National Vulnerability Database are linked to PHP. Still the language itself rarely has shown security flaws. Some settings of the PHP runtime and a lack by programmers of understanding of some pecualiarities of the language are usually at the root of the vulnerabilites.
PHP libraries/software
The source distribution of PHP includes many well-documented free and open source libraries covering the most typical internet-aware use cases of server-side programming, like extensive database connectivity, accessing files via FTP, connecting to other kind of servers and so on. It's also possible to write PHP extensions both in C or other more high-level languages. Of course many free and often open source libraries are available for PHP, but the main strength of PHP lies in the availabilities of some of the most popular and widely diffused whole platform packages like WordPress of the many bulletin board systems, e-shop solutions, most of which are totally free, open source and easily customizable. In fact the vast majority of web sites using PHP do it indirectly because of the adoption of these platforms.
Performance
The reference implementation of PHP is the Zend Engine which compiles at runtime PHP sources into an internal format and execute it, working like an interpreter, hence execution speed is usually poor. However, due to the huge adoption of PHP, even by some of the biggest companies of the world, the language has seen during the years the development of quite a number of alternative technologies to run its code in production environments. For example HHVM (HipHop Virtual Machine), developed at Facebook and available as open source, converts PHP code into x86-64 machine code dynamically at runtime with a just-in-time (JIT) compiler, gaining up to 6× execution speed improvements. Other virtual machines or transpilers to Java o CLI bytecode also exist, even if often these alternative execution environments do not retain the complete compatibility with the original PHP implementation of the Zend Engine.
Other features
Free and open source
PHP is free and open source. Its license, the PHP License, is approved by the Open Source Initiative but it's a non-copyleft free software license according to the Free Software Foundation. The license is GPL-incompatible becuse it forbids the use of the term PHP in derivative works.
Notable softwares/platforms/products written in PHP
- Facebook was written in PHP and most of it is still written in the PHP derivative language Hack created by Facebook
- Yahoo was interely written in PHP
- Wikipedia.org is written in PHP and Hack
- WordPress, the most deployed CMS of the world, is free and open-source, and is written in PHP
Best books for learning PHP
For beginners
Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 | |
Build interactive, data-driven websites Not only will you get up to speed on the most recent updates to these various technologies—including enhanced MySQL and PHP security features—you’ll also learn how... (continue on Amazon) |
For experienced programmers
PHP 7 Programming Cookbook | |
The most up-to-date book in the market on PHP It covers the new features of version 7.x, best practices for server-side programming, and MVC frameworks. The recipe-based approach will... (continue on Amazon) |
Conclusion
Since its inception many many decades ago the IT world has always been infested by harsh flamings and religious wars about platforms, programming languages, operating systems and so on. PHP has often been, and still today happens to be, target of many attacks for it's lack of... of what? Performance speed? A rigorous language design? Completeness of its functionalities? Elegance of it's constructs? Well, solved, solved, solved and solved. The point is: when something happens to be so easy to use that it allows you to build some of the biggest platforms of the world, with which you monetize so much and so big... you can solve all the problems a particular technology could throw at you. PHP is the most evident demonstration of this.