PHP - Development



Install PHP:

# install this first - or ``php`` will auto-install Apache
apt install php-cgi
apt install --no-install-recommends php

Check PHP version:

php -v
# PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)

Install useful modules:

apt install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath
# to list the installed modules
php -m

Composer for Dependency Management:

curl -sS -o /tmp/composer-setup.php

# check the hash
HASH=`curl -sS`
echo $HASH

# compare the hash to the one on this page

# check the script is safe to run
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

# install composer (global)
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

# check your composer install


To have separate Composer executable for each project, you can install it locally, on a per-project basis. To do this, use the command php /tmp/composer-setup.php without any arguments. This command will generate a composer.phar file in your current directory, which you can run using php composer.phar.


Using Composer in a PHP Project:

cd ~/dev/learn/
mkdir learn-php-composer
cd learn-php-composer/

composer init


The vendor folder should never be committed to your version control system. The vendor folder only contains packages you have installed from other vendors.

To check for outdated packages:

composer outdated --minor-only

And to update:

composer update

To install a package:

composer require --dev fakerphp/faker


Remove --dev if this is not only for development.

PHP Script

Test PHP Script:

# vim hello.php
  echo 'Hello World!';

Run the script:

php hello.php


Add phpunit to your project (using composer):

composer require --dev phpunit/phpunit
# check the version
./vendor/bin/phpunit --version

To run the tests (after installed phpunit using composer):

./vendor/bin/phpunit tests/GreeterTest.php


The tests script is from We can use Malcolm’s repository (which works nicely)


I reviewed PHPUnit, Chapter 8. Database Testing and it says,

PHPUnit assumes that the database schema with all its tables, triggers, sequences and views is created before a test is run. This means you as developer have to make sure that the database is correctly setup before running the suite.

There are several means to achieve this pre-condition to database testing.

If you are using a persistent database (not Sqlite Memory) you can easily setup the database once with tools such as phpMyAdmin for MySQL and re-use the database for every test-run.

So I think we can manually create the tables for now…



To auto-load a class i.e. no need to import it:



Use print_r for logs etc.

To assign the string created by print_r to a variable:

print_r($database, true);