How to
PHP 7
Install PHP 7.4 - Ubuntu below 20.04
sudo apt-get update
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt -y install php7.4 php7.4-xml composer php7.4-mbstring
PHP 8
Install PHP 8.0 - Ubuntu
sudo add-apt-repository ppa:ondrej/php --force-yes
sudo apt-get update ; \
sudo apt install php8.0-common php8.0-cli -y
php -v # Show PHP version.
php -m # Show PHP modules loaded.
Install additional extensions
An example to install a few more useful extensions:
sudo apt install php8.0-{bz2,curl,intl,mysql,readline,xml}
For development environments, code coverage tools or the Xdebug debugger can be installed as well.
sudo apt install php8.0-pcov # PCOV code coverage tool
sudo apt install php8.0-xdebug # Xdebug debugger
Purge old PHP versions
If the new installation is working as expected, you can remove the old PHP packages from the system.
sudo apt purge '^php7.4.*'
This assumes you are using PHP 7.4 as the previous version. Change php7.4
part of the command above with the appropriate PHP version.
References
Install Pear
sudo apt install php8.0-xml php-pear
pear install PHP_CodeSniffer-3.5.8
# or
# composer global require squizlabs/php_codesniffer:3.5.8
Commands
PHP Built-in Server
# Against current folder
php -S localhost:8888 -t .
Interactive mode
php -a
Set Default value for env vars
# Constants
define('DB_HOST', getenv('MY_DB_HOST') ?: 'my-default-database-name' );
# Variables
$host = getenv('MY_DB_HOST') ?: 'my-default-database-name';
Generate BCrypt hash
$ php -a
Interactive mode enabled
php > echo password_hash('Abcd123456', PASSWORD_DEFAULT);
$2y$10$id8o48ibJptfS7c6HL2x/uJNX4jBDSW.RYLAgx1HtG66EHvSJgM0K
Execute Shell Script from PHP
print_r(shell_exec("cat /etc/hosts"));die;
Working with Dates
$mytime = Carbon\Carbon::now();
echo $mytime->toDateTimeString();
Use PHPCS globally
Installing globally PHPCS
composer global require squizlabs/php_codesniffer
Know your path
echo $path
Add the line below into your
~/.zshrc
ou~/.bashrc
export PATH="/home/$USER/.config/composer/vendor/bin:$PATH"
array_map to utf8_encode
$result = array_map(function ($data) {
return array_map('utf8_encode', $data);
}, $result);
Last updated
Was this helpful?