PHP is a widely used, open source, general-purpose, multi-paradigm, dynamically typed and interpreted scripting language originally designed for server-side web development. Use this tag for questions about programming in the PHP language.
PHP is a widely used, open source, general-purpose, multi-paradigm, dynamically typed and interpreted scripting language originally designed for server-side web development.
The original PHP project, as invented by Rasmus Lerdorf, stood for Personal Home Page. Today, it stands for the recursive acronym PHP: Hypertext Preprocessor.
To create an environment for learning and experimenting with PHP, there are a number of application bundles that include – among other components – a web server and PHP:
- XAMPP (for Windows, Mac OS X & Linux)
- DEB.SURY.ORG (PHP For Debian in general & Ubuntu-based Linux distros. package and PPA)
- MAMP (for Mac OS)
- WampServer (for Windows)
- easyPHP (for Windows)
- WPN-XM (for Windows)
- Laragon (for Windows)
There are options like Cygwin (Linux on Windows) in which you can install PHP just like any other Linux
- Cygwin (Linux on Windows)
PHP provides a built-in web server for testing and development purposes. It can be started using the following command:
php -S localhost:8000
After executing the above command, the server will listen on port
8000 using the current working directory as its document root. See the PHP manual for more information.
Current Stable Version (8.1.x) : 8.1.3 // Release Date: 17 Feb 2022
Old Stable Version (8.0.x) : 8.0.16 // Release Date: 17 Feb 2022
Old Stable Version (7.4.x) : 7.4.28 // Release Date: 17 Feb 2022
It is recommended to use the current stable released version. All versions below 7.3 are officially unsupported and have been announced end-of-life. A list of supported branches and their maintenance status can be found here.
For further information about new features and required changes in a new version, see the official migration docs:
Sample PHP script
This script displays
Hello World! on your screen.
<?php echo 'Hello World!';
To run this script in a console, save it in the current working directory in a file called
hello.php and simply execute the command:
PHP has many active community forums, including:
The PHP manual is the official documentation for the language syntax featuring function search and URL shortcuts (for example, https://php.net/explode). The API is well documented for bundled and additional extensions. Most additional extensions can be found in PECL. The PEAR repository contains a plethora of community supplied classes. It is also possible to download an offline version of the documentation here.
Additionally, the PHP Framework Interop Group (PHP-FIG) has created sets of standards with regards to PHP coding styles and standards. These PHP Standard Recommendations (PSRs) can be found here.
PHP security-related information
Free PHP Programming Books
- PHP Essentials
- Practical PHP Programming (wiki containing O'Reilly's PHP In a Nutshell)
- Zend Framework: Survive the Deep End
- PHP: The Right Way (a community-driven quick reference for PHP best practices and accepted coding standards)
PHP supports a wide range of databases, relational and non-relational alike.
PHP is often paired with the MySQL relational database. PHP also includes great database support for PostgreSQL, SQLite, Microsoft SQL Server (API reference), Oracle, IBM DB2 & Cloudscape, Apache Derby, and even ODBC.
All modern versions of PHP include PDO: a built-in data-access abstraction library with comprehensive connectivity options. More recently, PECL extensions that offer "NoSQL" database support have surfaced, including Apache Thrift (for Apache Cassandra), MongoDB, Redis, and others.
Useful Third-party Code and Tools
In addition to the vast functionality provided in the PHP Core and through PEAR and PECL, there are a number of noteworthy third-party contributions to the PHP world, some of which are listed below:
Package Management with Composer
Composer is a package management tool for PHP inspired by npm for Node.js and Bundler for Ruby. It allows for per-project dependencies to be specified in a JSON file.
Composer uses packages from Packagist which is rapidly growing to contain many of the most popular PHP libraries.
Composer solves the following problems:
- You have a project that depends on a number of libraries.
- Some of those libraries depend on other libraries.
- You declare the things you depend on.
- Composer determines which versions of which packages need to be installed and downloads them into a directory (usually
vendor) in your project.
Nothing comes for free. Software downloaded with Composer may have bugs, just like any other, including security vulnerabilities. It is your responsibility to be aware of what you install and to update when necessary in order to get security fixes.
Quality Assurance Tools
- Behat (Test framework for Behavior Driven development)
- PEAR CodeSniffer
- more …
Coding standards and conventions
There are a number of coding standards that have been proposed and accepted by the PHP Framework Interop Group (PHP-FIG). These are known as the PHP Standards Recommendations (PSRs). As of July 2nd, 2017, the following recommendations are in effect:
- PSR-1 - Basic Coding Standard
- PSR-3 - Logger interface
- PSR-4 - Autoloading standard (this deprecates PSR-0)
- PSR-6 - Caching interface
- PSR-7 - HTTP message interfaces
- PSR-12 - Extended Coding Style
- PSR-13 - Link definition interfaces
- PSR-16 - Common Interface for Caching Libraries
A complete list of all recommendations alongside their status can be found on the PHP-FIG Recommendations page