install latest version of php code example

Example 1: install php 7.4 on ubuntu 20.04

sudo apt update
sudo apt install php7.4
sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y

Example 2: install php 7.4 ubuntu

sudo apt update
sudo apt install php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath

Example 3: latest php version

<?php
$json = file_get_contents('https://www.php.net/releases/?json');

$releases = [];
if ($json) $releases = json_decode($json, true);

/**
 * Versions
Array
(
    [0] => 7
    [1] => 5
    [2] => 4
    [3] => 3
)
*/
$versions = array_keys($releases);

/** solution to: latest php version */
if (array_key_exists(max($versions), $releases)) {
  echo $releases[max($versions)]['version']; // as of Aug 2020.. 7.4.9
}

Tags:

Php Example