PHP

In this part of the PHP tutorial, we talk about the PHP programming language in general.

Goal

The goal of this tutorial is to get you started with the PHP programming language. The tutorial covers the core of the PHP language: variables, arrays, control structures, and other core features. It does not cover web development, databases, or other numerous topics. In this tutorial, we cover PHP 5.6.

PHP

PHP is a scripting language designed for web development. It is used to produce dynamic web pages. Currently, PHP is one of the most widely used programming languages. Much of its syntax is borrowed from C, Java, and Perl with a couple of unique PHP-specific features. PHP can be embedded into HTML code and it generally runs on a web server. The web server needs to be configured to process PHP code and create web page content from it. It can be deployed on most web servers and operating systems. PHP is a free software. PHP was first created in 1995 and is actively developed since then. Today, there are two main branches of PHP: PHP 5 and PHP 7.

PHP supports both object oriented and procedural programming styles.

The official web site for the PHP programming language is php.net

Installing PHP

We download PHP from the http://php.net/downloads.php page. We have downloaded php-5.6.17.tar.bz2 file.

$ bunzip2 php-5.6.17.tar.bz2
$ tar -xf php-5.6.17.tar
$ cd php-5.6.17/

We decompress the file and move into the build directory.

$ sudo apt-get install libreadline-dev

We need to install readline development library for PHP interactive shell.

$ ./configure --with-readline

We run the configure script with readline support enabled.

$ make
$ sudo make install

We build and install PHP. It is possible that we need to install some additional packages. For instance, the author had to install libxml2-dev library.

$ php -a
Interactive mode enabled

php > echo phpversion();
5.6.17

We run PHP in interactive mode and get the version of PHP.

Another option is to install PHP from packages.

$ sudo apt-get install php5-cli

We install the php5-cli module (Debian-based Linux). Note that packages are usually older that the latest versions of the software. When we install PHP from the sources, the php5-cli module is included by default.

PHP CLI

PHP CLI is a command line interpreter for the PHP language. It is useful for testing PHP scripts from the shell. In this tutorial, we are using the PHP command line interpreter. We focus on the core of the PHP language.

simple.php
<?php

echo "this is PHP language\n";

?>

Here we have a simple PHP script.

$ php simple.php
this is PHP language

We execute the script.

Interactive shell

Like Python or Ruby, PHP also has an interactive shell. It is useful to test small language constructs.

$ php -a
Interactive mode enabled

php > print PHP_OS;
Linux
php > print PHP_VERSION;
5.6.17

The PHP shell is invoked with the -a option of the php command. The shell uses the php > prompt.

References

In this part of the PHP tutorial, we have introduced the PHP language.