1. Language Highlights
1.1 Xdebug
One of the most useful tools in software development is a proper debugger. It allows you to trace the execution of your code and monitor the contents of the stack. Xdebug, PHP’s debugger, can be utilized by various IDEs to provide Breakpoints and stack inspection. It can also allow tools like PHPUnit and KCacheGrind to perform code coverage analysis and code profiling.
If you find yourself in a bind, willing to resort to var_dump()/print_r(), and you still can’t find the solution -
maybe you need to use the debugger.
Installing Xdebug can be tricky, but one of its most important features is “Remote Debugging” - if you develop code locally and then test it inside a VM or on another server, Remote Debugging is the feature that you will want to enable right away.
Traditionally, you will modify your Apache VHost or .htaccess file with these values:
1 php_value xdebug.remote_host 192.168.?.?
2 php_value xdebug.remote_port 9000
The “remote host” and “remote port” will correspond to your local computer and the port that you configure your IDE to listen on. Then it’s just a matter of putting your IDE into “listen for connections” mode, and loading the URL:
1 http://your-website.example.com/index.php?XDEBUG_SESSION_START=1
Your IDE will now intercept the current state as the script executes, allowing you to set breakpoints and probe the values in memory.
Graphical debuggers make it very easy to step through code, inspect variables, and eval code against the live runtime. Many IDEs have built-in or plugin-based support for graphical debugging with Xdebug. MacGDBp is a free, open-source, stand-alone Xdebug GUI for macOS.
1.2 Command Line Interface
PHP was created to write web applications, but is also useful for scripting command line interface (CLI) programs. Command line PHP programs can help automate common tasks like testing, deployment, and application administration.
CLI PHP programs are powerful because you can use your app’s code directly without having to create and secure a web GUI for it. Just be sure not to put your CLI PHP scripts in your public web root!
Try running PHP from your command line:
1 > php -i
The -i option will print your PHP configuration just like the phpinfo() function.
The -a option provides an interactive shell, similar to ruby’s IRB or python’s interactive shell. There are a number
of other useful command line options, too.
Let’s write a simple “Hello, $name” CLI program. To try it out, create a file named hello.php, as below.
1 <?php
2 if ($argc !== 2) {
3 echo "Usage: php hello.php <name>" . PHP_EOL;
4 exit(1);
5 }
6 $name = $argv[1];
7 echo "Hello, $name" . PHP_EOL;
PHP sets up two special variables based on the arguments your script is run with. $argc is an integer
variable containing the argument count and $argv is an array variable containing each argument’s value.
The first argument is always the name of your PHP script file, in this case hello.php.
The exit() expression is used with a non-zero number to let the shell know that the command failed. Commonly used
exit codes can be found here.
To run our script, above, from the command line:
1 > php hello.php
2 Usage: php hello.php <name>
3 > php hello.php world
4 Hello, world
1.3 Standard PHP Library
The Standard PHP Library (SPL) is packaged with PHP and provides a collection of classes and interfaces. It is made up primarily of commonly needed datastructure classes (stack, queue, heap, and so on), and iterators which can traverse over these datastructures or your own classes which implement SPL interfaces.
1.4 Programming Paradigms
PHP is a flexible, dynamic language that supports a variety of programming techniques. It has evolved dramatically over the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP 5.3 (2009), and traits in PHP 5.4 (2012).
Object-oriented Programming
PHP has a very complete set of object-oriented programming features including support for classes, abstract classes, interfaces, inheritance, constructors, cloning, exceptions, and more.
Functional Programming
PHP supports first-class functions, meaning that a function can be assigned to a variable. Both user-defined and built-in functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other functions (a feature called Higher-order Functions) and functions can return other functions.
Recursion, a feature that allows a function to call itself, is supported by the language, but most PHP code is focused on iteration.
New anonymous functions (with support for closures) are present since PHP 5.3 (2009).
PHP 5.4 added the ability to bind closures to an object’s scope and also improved support for callables such that they can be used interchangeably with anonymous functions in almost all cases.
- Continue reading on Functional Programming in PHP
- Read about Anonymous Functions
- Read about the Closure class
- More details in the Closures RFC
- Read about Callables
- Read about dynamically invoking functions with
call_user_func_array()
Meta Programming
PHP supports various forms of meta-programming through mechanisms like the Reflection API and Magic Methods. There are
many Magic Methods available like __get(), __set(), __clone(), __toString(), __invoke(), etc. that allow
developers to hook into class behavior. Ruby developers often say that PHP is lacking method_missing, but it is
available as __call() and __callStatic().
1.5 Namespaces
As mentioned above, the PHP community has a lot of developers creating lots of code. This means that one library’s PHP code might use the same class name as another. When both libraries are used in the same namespace, they collide and cause trouble.
Namespaces solve this problem. As described in the PHP reference manual, namespaces may be compared to operating system directories that namespace files; two files with the same name may co-exist in separate directories. Likewise, two PHP classes with the same name may co-exist in separate PHP namespaces. It’s as simple as that.
It is important for you to namespace your code so that it may be used by other developers without fear of colliding with other libraries.
One recommended way to use namespaces is outlined in PSR-4, which aims to provide a standard file, class and namespace convention to allow plug-and-play code.
In October 2014 the PHP-FIG deprecated the previous autoloading standard: PSR-0. Both PSR-0 and PSR-4 are still perfectly usable. The latter requires PHP 5.3, so many PHP 5.2-only projects implement PSR-0.
If you’re going to use an autoloader standard for a new application or package, look into PSR-4.
1.6 Windows Setup
You can download the binaries from windows.php.net/download. After the extraction of PHP, it is recommended to set the PATH to the root of your PHP folder (where php.exe is located) so you can execute PHP from anywhere.
For learning and local development, you can use the built in webserver with PHP 5.4+ so you don’t need to worry about configuring it. If you would like an “all-in-one” which includes a full-blown webserver and MySQL too then tools such as the XAMPP, EasyPHP, OpenServer and WAMP will help get a Windows development environment up and running fast. That said, these tools will be a little different from production so be careful of environment differences if you are working on Windows and deploying to Linux.
If you need to run your production system on Windows, then IIS7 will give you the most stable and best performance. You can use phpmanager (a GUI plugin for IIS7) to make configuring and managing PHP simple. IIS7 comes with FastCGI built in and ready to go, you just need to configure PHP as a handler. For support and additional resources there is a dedicated area on iis.net for PHP.
Generally running your application on different environment in development and production can lead to strange bugs popping up when you go live. If you are developing on Windows and deploying to Linux (or anything non-Windows) then you should consider using a Virtual Machine.
Chris Tankersley has a very helpful blog post on what tools he uses to do PHP development using Windows.
1.7 macOS Setup
macOS 12 (Monterey) and later does not come prepackaged with PHP. Earlier macOS versions include PHP but are behind the latest stable release. There are multiple ways to install the latest PHP version on macOS.
Install PHP via Homebrew
Homebrew is a package manager for macOS that helps you easily install PHP and various extensions. The Homebrew core repository provides “formulae” for PHP 8.1, 8.2, 8.3 and 8.4. Install the latest version with this command:
1 brew install php
You can switch between Homebrew PHP versions by modifying your PATH variable. Alternatively, you can use brew-php-switcher to switch PHP versions automatically.
You can also switch between PHP versions manually by unlinking and linking the wanted version:
1 brew unlink php
2 brew link --overwrite php@8.2
1 brew unlink php
2 brew link --overwrite php@8.3
Install PHP via Macports
The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the macOS operating system.
MacPorts supports pre-compiled binaries, so you don’t need to recompile every dependency from the source tarball files, it saves your life if you don’t have any package installed on your system.
At this point, you can install php54, php55, php56, php70, php71, php72, php73, php74, php80, php81, php82 or php83 using the port install command, for example:
1 sudo port install php74
2 sudo port install php83
And you can run select command to switch your active PHP:
1 sudo port select --set php php83
Install PHP via phpbrew
phpbrew is a tool for installing and managing multiple PHP versions. This can be really useful if two different applications/projects require different versions of PHP, and you are not using virtual machines.
Install PHP via Liip’s binary installer
Another popular option is php-osx.liip.ch which provides one liner installation methods for versions 5.3 through 7.3. It doesn’t overwrite the PHP binaries installed by Apple, but installs everything in a separate location (/usr/local/php5).
Compile from Source
Another option that gives you control over the version of PHP you install, is to compile it yourself. In that case be sure to have installed either Xcode or Apple’s substitute “Command Line Tools for XCode” downloadable from Apple’s Developer Center.
All-in-One Installers
The solutions listed above mainly handle PHP itself, and do not supply things like Apache, Nginx or a SQL server. “All-in-one” solutions such as MAMP and XAMPP will install these other bits of software for you and tie them all together, but ease of setup comes with a trade-off of flexibility.
1.8 Linux Setup
Most GNU/Linux distributions come with PHP available from the official repositories, but those packages usually are a little behind the current stable version. There are multiple ways to get newer PHP versions on such distributions.
Ubuntu-based distributions
On Ubuntu and Debian-based GNU/Linux distributions, for instance, the best alternatives for native packages are provided and maintained by Ondřej Surý, through his Personal Package Archive (PPA) on Ubuntu and DPA/bikeshed on Debian. Find instructions for each of these below.
For Ubuntu distributions, the PPA by Ondřej Surý provides supported PHP versions along with many PECL extensions. To add this PPA to your system, perform the following steps in your terminal:
- First, add the PPA to your system’s software sources using the command:
1sudo add-apt-repository ppa:ondrej/php - After adding the PPA, update your system’s package list:
1sudo apt update
This will ensure that your system can access and install the latest PHP packages available in the PPA.
Debian-based distributions
For Debian-based distributions, Ondřej Surý also provides a bikeshed (Debian equivalent of a PPA). To add the bikeshed to your system and update it, follow these steps:
- Ensure that you have root access. If not, you might need to use
sudofor the following commands. - Update your system’s package list:
1sudo apt-get update - Install
lsb-release,ca-certificates, andcurl:1sudo apt-get -y install lsb-release ca-certificates curl - Download the signing key for the repository:
1sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg - Add the repository to your system’s software sources:
1sudo sh -c'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.su\2ry.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' - Finally, update your system’s package list again:
1sudo apt-get update
With these steps, your system will be able to install the latest PHP packages from the bikeshed.
RPM-based distributions
On RPM-based distributions (CentOS, Fedora, RHEL, etc.) you can use the Remi’s RPM repository to install the latest PHP version or to have multiple PHP versions simultaneously available.
There is a configuration wizard available to configure your RPM-based distribution.
All that said, you can always use containers or compile the PHP source code from scratch.