Php

PHP

PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

See PHP on Wikipedia.

Getting started

The Pro Linux Container of Brap is pre-configured with PHP.

Here is a Hello, World! program in PHP:

<?php
    echo 'Hello, World!';
?>

To run a PHP application, run:

php hello-world.php

Since PHP 5.4, we can use the built-in and basic web-server of PHP to aid in our application development, testing, or demonstration purposes.

To serve a PHP application to the web, run:

php -S 0.0.0.0:8000 hello-world.php

To serve a PHP application directory, without a specific file, run:

php -S 0.0.0.0:8000 /example/php/hello-world

The /example/php/hello-world directory must have either an index.php or index.html file.

To serve a PHP application directory with a specific file, run:

php -S 0.0.0.0:8000 /example/php/hello-world/ alternate-file.php

PHP in VS Code terminal

Screenshot: PHP in VS Code terminal

PHP running in the web browser

Screenshot: PHP running in the web browser

Manage PHP versions

Our Pro Linux Container is pre-installed with the following PHP versions via ppa:ondrej/php:

  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • PHP 7.2
  • PHP 7.3
  • PHP 7.4
  • PHP 8.0
  • PHP 8.1
  • PHP 8.2
  • PHP 8.3

To check which PHP version is currently used by the CLI, run:

php -v

To change the PHP version used by the CLI, run any of the following:

sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set php /usr/bin/php7.1
sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set php /usr/bin/php7.3
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set php /usr/bin/php8.0
sudo update-alternatives --set php /usr/bin/php8.1
sudo update-alternatives --set php /usr/bin/php8.2
sudo update-alternatives --set php /usr/bin/php8.3

To list the installed PHP versions and select which one to use, run:

sudo update-alternatives --config php

To install another PHP version, such as PHP 5.3 and its recommended extensions, run:

apt install php5.3 php5.3-fpm php5.3-mysql php5.3-mbstring php5.3-xml php5.3-bcmath php5.3-zip php5.3-curl php5.3-gd

Keywords

  • php
  • high level
  • backend
  • web
  • fullstack

Back to top