Image

php

Cheatsheet

1 min read
Last update: November 28, 2021

Switch between PHP versions

Source: stackexchange.com/how-to-switch-between-php-versions-on-ubuntu-nginx

If you want to switch PHP CLI version, follow these steps

If you just want to use another PHP version pour a Virtual host, like a old Drupal app, just change PHP version in NGINX conf.
sudo update-alternatives --config php

You will have a menu to choose PHP CLI version

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.4   74        auto mode
  1            /usr/bin/php7.2   72        manual mode
  2            /usr/bin/php7.3   73        manual mode
  3            /usr/bin/php7.4   74        manual mode

After change PHP version restart NGINX and restart PHP version you choose

sudo service nginx restart
# example if you choose php7.2
sudo service php7.2-fpm restart

Syntax

Concat variable into string:

<?php

$basic = "lists/".$list_test_id."/contacts"
<?php

$improved = "lists/${list_test_id}/contacts"

Errors

Error on function where everything is okay about syntax.

Type declaration

Check return data like:

<?php

public function getData : Collection
{
    $data = Data::all();
    return $data;
}

It will works only with PHP 7.3, maybe NGINX VHost use a PHP version too old.