💡
Knowledge
  • Home
  • Technology
    • Browser
      • Chrome / Brave
        • Known Issues
    • Messaging
      • Discord
        • Known Issues
      • Microsoft Teams
      • Telegram
    • Office Production
      • Sheets
    • Music Production
    • Operational systems
      • Docker + OSX
      • Raspberry Pi OS
      • Linux
        • Terminal
        • Known Issues
        • Desktop Environment
          • Gnome
            • How to
            • Known Issues
            • Theming
          • Kde Plasma
        • How to
          • Audio
          • Proxy
          • SSH
          • ZSH
      • Windows
    • Programming
      • Code Quality
        • Stress Tests
      • Cascading Style Sheets(CSS)
      • Database
        • Postgres
        • SQLServer
      • Design Patterns
      • DevOps
        • Cloud Platforms
        • Continuous Integration
        • Docker
          • How to
          • IPSEC VPN Server
          • Docker Compose
          • Known issues
          • Proxy
        • Swarm
      • Git
        • How to
        • Known Issues
        • Github
        • Gitlab
          • GitlabCI
          • Gitlab Runner
      • IDE / Text Editor
        • Vim
          • Commands
        • PHPStorm
        • VSCode
      • Programming Languages
        • Typescript
        • Java
          • How to
          • Spring Boot
        • Javascript
          • Known issues
          • Backend
            • NestJS
            • NodeJS
          • Frontend
            • JQuery
            • React
            • Vue
          • How to
          • Package Manager
            • Yarn
          • Packages
          • Vanilla
        • PHP
          • About
          • Cache
          • Composer
          • Docker
          • How to
          • Known Issues
          • Laravel
            • Jet Stream
            • Know Issues
            • Sanctum
            • Sail
            • Valet
          • Tools
            • PHPUnit
          • Wordpress
            • Docker
            • WP CLI
            • Known Issues
            • WooComerce
        • Python
        • Shell Script
      • Server
        • Apache2
          • Known Issues
        • Nginx
          • How To
          • Known issues
      • Tools
        • Visual Studio Code
    • Stream
      • Game
      • Twitch
      • Tests
        • Unit Tests
    • Sites
    • Specs
    • Tools
  • Pessoal
    • About me
Powered by GitBook
On this page
  • PHP 7
  • Install PHP 7.4 - Ubuntu below 20.04
  • PHP 8
  • Install PHP 8.0 - Ubuntu
  • Install additional extensions
  • Purge old PHP versions
  • Install Pear
  • Commands
  • PHP Built-in Server
  • Interactive mode
  • Set Default value for env vars
  • Generate BCrypt hash
  • Execute Shell Script from PHP
  • Working with Dates
  • Use PHPCS globally
  • array_map to utf8_encode

Was this helpful?

  1. Technology
  2. Programming
  3. Programming Languages
  4. PHP

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}
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);

PreviousDockerNextKnown Issues

Last updated 4 years ago

Was this helpful?

For development environments, or the Xdebug debugger can be installed as well.

code coverage tools
PHP.Watch