Learning PHP, part 1

In this article series, we'll look into the PHP language in depth, from scratch

By: Ajdin Imsirovic 17 November 2019

In this article, we’ll cover some PHP basics, namely using the REPL, and giving a high-level overview of what PHP is usually used for.

Learning PHP, part 1 Image by codingexercises

We’ll begin this series by specifying the system we’ll be working on, which is Ubuntu 18.04.

Sorry, no Windows! You can set up Ubuntu using a live USB.

Installing PHP 7.4 on Ubuntu 18.04

Assuming we have a fresh installation of Ubuntu 18.04, let’s test if the php command exists on the command line:

php

Note: this will not work.

Updating and upgrading our Ubuntu installation

If the Ubuntu 18.04 installation is brand new, we’ll get this message:

Command 'php' not found, but can be installed with:

sudo apt install php7.2-cli
sudo apt install hhvm

We won’t follow the above advice; instead, we’ll first update our local packages with apt-get:

sudo apt-get update -y;

This will download the current updates for our version of Ubuntu, from archive.ubuntu.com website. Once downloaded, we can install these new pacakges:

sudo apt-get upgrade -y;

We’ll see a lot of this:

preparing to unpack...
unpacking...
setting up...
processing triggers...

Adding a user for php

The sudo user comes with Ubuntu, but we need to add our own with adduser <someusername>, for example:

adduser phpuser

Running the above will result in this:

adduser: Only root may add a user or group to the system.

This means we need to run it like this:

sudo adduser phpuser

Now we’ll get this back:

[sudo] password for <your-computer-name>: 
Adding user `phpuser' ...
Adding new group `phpuser' (1001) ...
Adding new user `phpuser' (1001) with group `phpuser' ...
Creating home directory `/home/phpuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 

Let’s try to cheat by pressing the ENTER key; this means we want to leave the password blank. Now it will ask us this:

Retype new UNIX password: 

Let’s press the ENTER key again; now we’ll get this message:

No password supplied

It will ask us for password again, so let now provide a password. If you’re lazy, you can just use the same password as for the sudo user; however, note that this is not the best practice as far as security goes; you really should use a different password. The problem here is, you might forget it. Anyway, let’s continue with the user addition process:

Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for phpuser
Enter the new value, or press ENTER for the default
	Full Name []: <press ENTER here>
	Room Number []: <press ENTER here>
	Work Phone []: <press ENTER here>
	Home Phone []: <press ENTER here>
	Other []: <press ENTER here>
Is the information correct? [Y/n] <press ENTER here>

We’ve pressed the ENTER key six times, and thus accepted all the default information about our phpuser user.

Now we’ll add our new user to the sudo group:

usermod -aG sudo phpuser

Now we’ll get an error again:

usermod: Permission denied.
usermod: cannot lock /etc/passwd; try again later.

This means we need to run it with sudo, so:

sudo usermod -aG sudo phpuser

Alternatively, we can run it with sudo !!, because the double exclamation mark is the alias for “previous command”, so effectively, it’s like typing: sudo usermod -aG sudo phpuser.

Now we’ll close the system with keyboard combo WIN + L (same as on Windows, this command closes the desktop). Now we can re-login as our new user, and test how our user works:

ls -la /root

This will throw an error:

ls: cannot open directory '/root': Permission denied

Now we can run it with sudo and it will work:

sudo ls -la /root

This takes care of the first task: setting up a user for php. Next, we’ll install a server on our machine.

Installing a server (Apache or Nginx)

We have a choice of either Apache or Nginx.

This command installs Apache:

sudo apt-get install apache2 -y

…and this command starts it:

sudo systemctl start apache2.service

Alternatively, you can install Nginx:

sudo apt-get install nginx -y

…and start it like this:

sudo systemctl start nginx.service

Installing PHP 7.2 on Ubuntu 18.04

Why PHP 7.2 specifically? Because it comes pre-packages with the Ubuntu 18.04 installation. It’s part of the repo for this version of Ubuntu.

Using the handy grep command we can pipe the available package names for PHP 7.2:

apt-cache pkgnames | grep php7.2

This is what we’ll get back:

php7.2-bz2
php7.2-common
php7.2-cgi
php7.2-cli
php7.2-dba
php7.2-dev
libphp7.2-embed
php7.2-bcmath
php7.2-fpm
php7.2-gmp
php7.2-mysql
php7.2-tidy
php7.2
php7.2-sqlite3
php7.2-json
php7.2-opcache
php7.2-sybase
php7.2-curl
php7.2-ldap
php7.2-phpdbg
php7.2-imap
php7.2-xml
php7.2-xsl
php7.2-intl
php7.2-zip
php7.2-odbc
php7.2-mbstring
php7.2-readline
php7.2-gd
php7.2-interbase
php7.2-snmp
php7.2-xmlrpc
php7.2-soap
php7.2-pspell
php7.2-pgsql
php7.2-enchant
php7.2-recode
libapache2-mod-php7.2

Before we can install any of the listed pacakges, we’ll need to add php itself:

sudo apt-get install php -y

Of course, we’ll be asked to provide the sudo password. If successfull, the command we just ran will start executing. The output for the above command will be huge. Here’s a sample output:

[sudo] password for phpuser: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gyp libjs-async libjs-inherits libjs-node-uuid libjs-underscore
  libpython-stdlib libuv1-dev node-abbrev node-ansi node-ansi-color-table
  node-archy node-async node-balanced-match node-block-stream
  node-brace-expansion node-combined-stream node-concat-map node-cookie-jar
  node-delayed-stream node-forever-agent node-form-data node-fs.realpath
  node-fstream node-fstream-ignore node-github-url-from-git node-glob
  node-graceful-fs node-hosted-git-info node-inflight node-inherits node-ini
  node-isexe node-json-stringify-safe node-lockfile node-lru-cache node-mime
  node-minimatch node-mkdirp node-mute-stream node-node-uuid node-nopt
  node-npmlog node-once node-osenv node-path-is-absolute node-pseudomap
  node-qs node-read node-request node-retry node-rimraf node-semver node-sha
  node-slide node-spdx-correct node-spdx-expression-parse
  node-spdx-license-ids node-tar node-tunnel-agent node-underscore
  node-validate-npm-package-license node-which node-wrappy node-yallist python
  python-minimal python-pkg-resources python2.7 python2.7-minimal
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.2 libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 php-common
  php7.2 php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.2 libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 php
  php-common php7.2 php7.2-cli php7.2-common php7.2-json php7.2-opcache
  php7.2-readline
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 5579 kB of archives.
After this operation, 24,1 MB of additional disk space will be used.
Get:1 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 libapr1 amd64 1.6.3-2 [90,9 kB]
Get:2 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 libaprutil1 amd64 1.6.1-2 [84,4 kB]
Get:3 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 libaprutil1-dbd-sqlite3 amd64 1.6.1-2 [10,6 kB]
Get:4 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 libaprutil1-ldap amd64 1.6.1-2 [8764 B]
Get:5 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 liblua5.2-0 amd64 5.2.4-1.1build1 [108 kB]
Get:6 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2-bin amd64 2.4.29-1ubuntu4.13 [1070 kB]
Get:7 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2-utils amd64 2.4.29-1ubuntu4.13 [83,8 kB]
Get:8 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2-data all 2.4.29-1ubuntu4.13 [160 kB]
Get:9 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2 amd64 2.4.29-1ubuntu4.13 [95,1 kB]
Get:10 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 php-common all 1:60ubuntu1 [12,1 kB]
Get:11 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 php7.2-common amd64 7.2.24-0ubuntu0.18.04.6 [888 kB]
Get:12 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 php7.2-json amd64 7.2.24-0ubuntu0.18.04.6 [18,9 kB]
Get:13 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 php7.2-opcache amd64 7.2.24-0ubuntu0.18.04.6 [165 kB]
Get:14 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 php7.2-readline amd64 7.2.24-0ubuntu0.18.04.6 [12,2 kB]
Get:15 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 php7.2-cli amd64 7.2.24-0ubuntu0.18.04.6 [1408 kB]
Get:16 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libapache2-mod-php7.2 amd64 7.2.24-0ubuntu0.18.04.6 [1350 kB]
Get:17 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 php7.2 all 7.2.24-0ubuntu0.18.04.6 [9244 B]
Get:18 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 php all 1:7.2+60ubuntu1 [3084 B]
Fetched 5579 kB in 1s (4271 kB/s)
Selecting previously unselected package libapr1:amd64.
(Reading database ... 188069 files and directories currently installed.)
Preparing to unpack .../00-libapr1_1.6.3-2_amd64.deb ...
Unpacking libapr1:amd64 (1.6.3-2) ...
Selecting previously unselected package libaprutil1:amd64.
Preparing to unpack .../01-libaprutil1_1.6.1-2_amd64.deb ...
Unpacking libaprutil1:amd64 (1.6.1-2) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3:amd64.
Preparing to unpack .../02-libaprutil1-dbd-sqlite3_1.6.1-2_amd64.deb ...
Unpacking libaprutil1-dbd-sqlite3:amd64 (1.6.1-2) ...
Selecting previously unselected package libaprutil1-ldap:amd64.
Preparing to unpack .../03-libaprutil1-ldap_1.6.1-2_amd64.deb ...
Unpacking libaprutil1-ldap:amd64 (1.6.1-2) ...
Selecting previously unselected package liblua5.2-0:amd64.
Preparing to unpack .../04-liblua5.2-0_5.2.4-1.1build1_amd64.deb ...
Unpacking liblua5.2-0:amd64 (5.2.4-1.1build1) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../05-apache2-bin_2.4.29-1ubuntu4.13_amd64.deb ...
Unpacking apache2-bin (2.4.29-1ubuntu4.13) ...
Selecting previously unselected package apache2-utils.
Preparing to unpack .../06-apache2-utils_2.4.29-1ubuntu4.13_amd64.deb ...
Unpacking apache2-utils (2.4.29-1ubuntu4.13) ...
Selecting previously unselected package apache2-data.
Preparing to unpack .../07-apache2-data_2.4.29-1ubuntu4.13_all.deb ...
Unpacking apache2-data (2.4.29-1ubuntu4.13) ...
Selecting previously unselected package apache2.
Preparing to unpack .../08-apache2_2.4.29-1ubuntu4.13_amd64.deb ...
Unpacking apache2 (2.4.29-1ubuntu4.13) ...
Selecting previously unselected package php-common.
Preparing to unpack .../09-php-common_1%3a60ubuntu1_all.deb ...
Unpacking php-common (1:60ubuntu1) ...
Selecting previously unselected package php7.2-common.
Preparing to unpack .../10-php7.2-common_7.2.24-0ubuntu0.18.04.6_amd64.deb ...
Unpacking php7.2-common (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package php7.2-json.
Preparing to unpack .../11-php7.2-json_7.2.24-0ubuntu0.18.04.6_amd64.deb ...
Unpacking php7.2-json (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package php7.2-opcache.
Preparing to unpack .../12-php7.2-opcache_7.2.24-0ubuntu0.18.04.6_amd64.deb ...
Unpacking php7.2-opcache (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package php7.2-readline.
Preparing to unpack .../13-php7.2-readline_7.2.24-0ubuntu0.18.04.6_amd64.deb ...
Unpacking php7.2-readline (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package php7.2-cli.
Preparing to unpack .../14-php7.2-cli_7.2.24-0ubuntu0.18.04.6_amd64.deb ...
Unpacking php7.2-cli (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package libapache2-mod-php7.2.
Preparing to unpack .../15-libapache2-mod-php7.2_7.2.24-0ubuntu0.18.04.6_amd64.deb ...
Unpacking libapache2-mod-php7.2 (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package php7.2.
Preparing to unpack .../16-php7.2_7.2.24-0ubuntu0.18.04.6_all.deb ...
Unpacking php7.2 (7.2.24-0ubuntu0.18.04.6) ...
Selecting previously unselected package php.
Preparing to unpack .../17-php_1%3a7.2+60ubuntu1_all.deb ...
Unpacking php (1:7.2+60ubuntu1) ...
Setting up libapr1:amd64 (1.6.3-2) ...
Setting up apache2-data (2.4.29-1ubuntu4.13) ...
Setting up libaprutil1:amd64 (1.6.1-2) ...
Setting up php-common (1:60ubuntu1) ...
Created symlink /etc/systemd/system/timers.target.wants/phpsessionclean.timer → /lib/systemd/system/phpsessionclean.timer.
Setting up liblua5.2-0:amd64 (5.2.4-1.1build1) ...
Setting up libaprutil1-ldap:amd64 (1.6.1-2) ...
Setting up php7.2-common (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/calendar.ini with new version

Creating config file /etc/php/7.2/mods-available/ctype.ini with new version

Creating config file /etc/php/7.2/mods-available/exif.ini with new version

Creating config file /etc/php/7.2/mods-available/fileinfo.ini with new version

Creating config file /etc/php/7.2/mods-available/ftp.ini with new version

Creating config file /etc/php/7.2/mods-available/gettext.ini with new version

Creating config file /etc/php/7.2/mods-available/iconv.ini with new version

Creating config file /etc/php/7.2/mods-available/pdo.ini with new version

Creating config file /etc/php/7.2/mods-available/phar.ini with new version

Creating config file /etc/php/7.2/mods-available/posix.ini with new version

Creating config file /etc/php/7.2/mods-available/shmop.ini with new version

Creating config file /etc/php/7.2/mods-available/sockets.ini with new version

Creating config file /etc/php/7.2/mods-available/sysvmsg.ini with new version

Creating config file /etc/php/7.2/mods-available/sysvsem.ini with new version

Creating config file /etc/php/7.2/mods-available/sysvshm.ini with new version

Creating config file /etc/php/7.2/mods-available/tokenizer.ini with new version
Setting up libaprutil1-dbd-sqlite3:amd64 (1.6.1-2) ...
Setting up apache2-utils (2.4.29-1ubuntu4.13) ...
Setting up apache2-bin (2.4.29-1ubuntu4.13) ...
Setting up php7.2-readline (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/readline.ini with new version
Setting up php7.2-json (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/json.ini with new version
Setting up php7.2-opcache (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/opcache.ini with new version
Setting up apache2 (2.4.29-1ubuntu4.13) ...
Enabling module mpm_event.
Enabling module authz_core.
Enabling module authz_host.
Enabling module authn_core.
Enabling module auth_basic.
Enabling module access_compat.
Enabling module authn_file.
Enabling module authz_user.
Enabling module alias.
Enabling module dir.
Enabling module autoindex.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module filter.
Enabling module deflate.
Enabling module status.
Enabling module reqtimeout.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Setting up php7.2-cli (7.2.24-0ubuntu0.18.04.6) ...
update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar7.2 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar7.2 to provide /usr/bin/phar.phar (phar.phar) in auto mode

Creating config file /etc/php/7.2/cli/php.ini with new version
Setting up libapache2-mod-php7.2 (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/apache2/php.ini with new version
Module mpm_event disabled.
Enabling module mpm_prefork.
apache2_switch_mpm Switch to prefork
apache2_invoke: Enable module php7.2
Setting up php7.2 (7.2.24-0ubuntu0.18.04.6) ...
Setting up php (1:7.2+60ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.41) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
ureadahead will be reprofiled on next reboot
Processing triggers for libc-bin (2.27-3ubuntu1) ...

Now we can install those packages that we’ll need for the specific php application we’re using/building. For example:

sudo apt-get install php-{common,cgi,cli,dba,dev,sqlite3,json} -y

Here’s an abbreviated output of the above command:

Reading package lists... Done
Building dependency tree
The following packages were automatically installed and are no longer required:
	<list-of-packages>
Use 'sudo apt autoremove' to remove them
The following additional packages will be installed:
	<list-of-packages>
Suggested packages:
	<list-of-packages>
The following NEW packages will be installed:
	<list-of-packages>
0 upgraded, 30 newly installed, 0 to remove and 0 not upgraded.
Need to get 5452 kB of archives.
After this operation, 27,6 MB of additional disk space will be used.
Get:1 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 autopoint all 0.19.8.1-6ubuntu0.3 [426kB]
Get:2 ...
Get:3 ...
Get:30 ...
Fetched 5452 kB in 2s
Selecting previously unselected package autopoint.

(Reading database ... 188912 files and directories currently installed.)
Preparing to unpack .../00-autopoint_0.19.8.1-6ubuntu0.3_all.deb ...
Unpacking autopoint (0.19.8.1-6ubuntu0.3) ...
Selecting previously unselected package libtool.

...

Unpacking pkg-php-tools (1.35ubuntu1) ...
Setting up libtool (2.4.6-2) ...
Setting up php7.2-xml (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/dom.ini with new version

Creating config file /etc/php/7.2/mods-available/simplexml.ini with new version

Creating config file /etc/php/7.2/mods-available/wddx.ini with new version

Creating config file /etc/php/7.2/mods-available/xml.ini with new version

Creating config file /etc/php/7.2/mods-available/xmlreader.ini with new version

Creating config file /etc/php/7.2/mods-available/xmlwriter.ini with new version

Creating config file /etc/php/7.2/mods-available/xsl.ini with new version
Setting up php7.2-sqlite3 (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/sqlite3.ini with new version

Creating config file /etc/php/7.2/mods-available/pdo_sqlite.ini with new version
Setting up libltdl-dev:amd64 (2.4.6-2) ...
Setting up po-debconf (1.0.20) ...
Setting up shtool (2.0.8-9) ...
Setting up libarchive-cpio-perl (0.10-1) ...
Setting up libqdbm14 (1.8.78-6.1ubuntu2) ...
Setting up php-xml (1:7.2+60ubuntu1) ...
Setting up php-cli (1:7.2+60ubuntu1) ...
Setting up libsys-hostname-long-perl (1.5-1) ...
Setting up libmail-sendmail-perl (0.80-1) ...
Setting up php-json (1:7.2+60ubuntu1) ...
Setting up php-pear (1:1.10.5+submodules+notgz-1ubuntu1.18.04.1) ...
Setting up libpcrecpp0v5:amd64 (2:8.39-9) ...
Setting up libpcre32-3:amd64 (2:8.39-9) ...
Setting up php7.2-cgi (7.2.24-0ubuntu0.18.04.6) ...
update-alternatives: using /usr/bin/php-cgi7.2 to provide /usr/bin/php-cgi (php-cgi) in auto mode
update-alternatives: using /usr/lib/cgi-bin/php7.2 to provide /usr/lib/cgi-bin/php (php-cgi-bin) in auto mode

Creating config file /etc/php/7.2/cgi/php.ini with new version
php_invoke: Enabled module sysvmsg for cgi sapi
php_invoke: Enabled module exif for cgi sapi
php_invoke: Enabled module sysvshm for cgi sapi
php_invoke: Enabled module iconv for cgi sapi
php_invoke: Enabled module fileinfo for cgi sapi
php_invoke: Enabled module json for cgi sapi
php_invoke: Enabled module gettext for cgi sapi
php_invoke: Enabled module phar for cgi sapi
php_invoke: Enabled module ftp for cgi sapi
php_invoke: Enabled module readline for cgi sapi
php_invoke: Enabled module posix for cgi sapi
php_invoke: Enabled module tokenizer for cgi sapi
php_invoke: Enabled module opcache for cgi sapi
php_invoke: Enabled module sysvsem for cgi sapi
php_invoke: Enabled module ctype for cgi sapi
php_invoke: Enabled module calendar for cgi sapi
php_invoke: Enabled module sockets for cgi sapi
php_invoke: Enabled module shmop for cgi sapi
php_invoke: Enabled module pdo for cgi sapi
Setting up libpcre16-3:amd64 (2:8.39-9) ...
Setting up php-sqlite3 (1:7.2+60ubuntu1) ...
Setting up autopoint (0.19.8.1-6ubuntu0.3) ...
Setting up libfile-stripnondeterminism-perl (0.040-1.1~build1) ...
Setting up libpcre3-dev:amd64 (2:8.39-9) ...
Setting up php-cgi (1:7.2+60ubuntu1) ...
Setting up php7.2-dba (7.2.24-0ubuntu0.18.04.6) ...

Creating config file /etc/php/7.2/mods-available/dba.ini with new version
Setting up php7.2-dev (7.2.24-0ubuntu0.18.04.6) ...
update-alternatives: using /usr/bin/php-config7.2 to provide /usr/bin/php-config (php-config) in auto mode
update-alternatives: using /usr/bin/phpize7.2 to provide /usr/bin/phpize (phpize) in auto mode
Setting up php-dev (1:7.2+60ubuntu1) ...
Setting up dh-autoreconf (17) ...
Setting up debhelper (11.1.6ubuntu2) ...
Setting up pkg-php-tools (1.35ubuntu1) ...
Setting up dh-strip-nondeterminism (0.040-1.1~build1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libapache2-mod-php7.2 (7.2.24-0ubuntu0.18.04.6) ...

Now we’ll need to restart the server.

Restart Apache or Ngnix

To restart Apache, run:

systemctl restart apache2.service

…and to restart Nginx, run:

systemctl restart nginx.service

Let’s now run a command to check what version our php is in:

php --version

We’ll get back version 7.2 and some additional details.

Configuring our PHP 7.2 installation on Ubuntu 18.04

The default configuration can be adjusted in the following file:

/etc/php/7.2/fpm/php.ini

To edit it, we can run this command:

sudo nano /etc/php/7.2/fpm/php.ini

After adjusting the php.ini file, we’ll have to run the restart command again on our server, as explained earlier.

That’s it, we’ve setup our php.

Running the PHP REPL

To run PHP repl, we’ll use this command:

php -a

Once REPL opens, we can run commands like:

echo "1 + 1";

The above command will print the following to the screen:

1 + 1

Let’s run it again, only this time without parentheses:

echo 1 + 1;

Now our output is the number two:

2

In the next article, we’ll learn how to setup PHP locally on Ubuntu 18.04, types in php, and we’ll learn about operators in PHP.

Feel free to check out my work here: