Composer tool itself

Updated: 27 July 2024

Install Composer phar

curl -sS https://getcomposer.org/installer | php

Circumvent php memory limit

php -d memory_limit=-1 composer.phar install

Up / downgrade Composer itself

composer self-update --help
composer self-update --1
composer self-update --2

Composer in Docker

Updated: 17 January 2023

Require a new project as a dependency

docker run --rm \
    -v $(pwd):/app \
    composer/composer:latest \
    require slim/slim "^3.0"

Install all dependencies in a project

docker run --rm --interactive --tty --volume $PWD:/app composer install

PHP in Docker

Updated: 17 May 2024

Pre-configured Docker images (including Xdebug) from Jetbrains https://github.com/JetBrains/phpstorm-docker-images


Run a php script

docker run -it --rm --name my-running-script \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
php:7.4-cli php script.php

Run the php development server

docker run -it --rm --name php-server \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
-p 8000:8000 \
php:8.2-cli php -S 0.0.0.0:8000

PHP deprecations, notices, warnings & errors

Updated: 26 November 2024

Attempt to activate and display all errors, warnings, deprecations and notices

error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', 'php-error.log')

// check the settings above
trigger_error('This is E_USER_DEPRECATED', E_USER_DEPRECATED);
trigger_error('This is E_USER_NOTICE', E_USER_NOTICE);
trigger_error('This is E_USER_WARNING', E_USER_WARNING);
trigger_error('This is E_USER_ERROR', E_USER_ERROR);

This script demonstrates how to temporarily adjust which PHP errors are reported, to avoid displaying a deprecation notice to users

// Activate every error, warning, deprecation & notice.
error_reporting(E_ALL);

// Get the current error reporting value.
$error_reporting = error_reporting();

// Reset the error reporting value but now excluding E_DEPRECATED.
error_reporting($error_reporting & ~E_DEPRECATED);

// With PHP 7.4, trigger an E_DEPRECATED.
get_magic_quotes_gpc();
// No deprecation message.

// Reset error reporting to original value.
error_reporting($error_reporting);

// Attempt to trigger an E_DEPRECATED.
get_magic_quotes_gpc();
// Deprecation message here.

PHP

Updated: 26 November 2024

PHP Local Security Checker
https://github.com/fabpot/local-php-security-checker


Check all extensions

php -m

Disable the xdebug module for the fpm sapi and php version 5.6

sudo phpdismod -v 5.6 -s fpm -m xdebug

Enable the xdebug module for the fpm sapi and php version 7.0

sudo phpenmod -v 7.0 -s fpm -m xdebug

Check the status of the xdebug module for the fpm sapi and php 7.4

phpquery -v 7.4 -s fpm -m xdebug

Restart the PHP 5.6 Fast Process Manager

sudo systemctl restart php5.6-fpm

PHP date and time

Updated: 26 November 2024

Create a new object

$date = new DateTime('2000-01-01 20:15:05');

/*var_dump($date);
object(DateTime)#649 (3) {
  ["date"]=>
  string(26) "2000-01-01 20:15:05.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}*/

Parse textual datetime description into a Unix timestamp

strtotime(string $dateTimeString, ?int $baseTimestamp = null): int|false

Convert date string to another format

$time = strtotime('11/17/2005');
// var_dump($time);
// int(1132185600)

$newformat = date('d F Y', $time);
// var_dump($newformat);
// string(16) "17 November 2005"

Format a DateTime object as a string

$datetime = new DateTime();
$formatted = $datetime->format('Y-m-d H:i:s');
// var_dump($formatted);
// string(19) "2024-02-23 16:16:24"

Sort multi-dimensional array by value of a DateTime element

$mydates = [
    ['datetime' => new \DateTime('2000-01-03')],
    ['datetime' => new \DateTime('2000-01-01 12:15')],
    ['datetime' => new \DateTime('2000-01-01 12:15:30')]
];
        
usort($mydates, function ($a, $b) {
    return $a['datetime'] < $b['datetime'];
});

/*print_r($mydates);
Array
(
    [0] => Array
        (
            [datetime] => DateTime Object
                (
                    [date] => 2000-01-03 00:00:00.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

        )

    [1] => Array
        (
            [datetime] => DateTime Object
                (
                    [date] => 2000-01-01 12:15:30.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

        )

    [2] => Array
        (
            [datetime] => DateTime Object
                (
                    [date] => 2000-01-01 12:15:00.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

        )
)*/

Symfony console

Updated: 23 November 2024

Generate a migration by comparing your current database to your mapping information

bin/console doctrine:migrations:diff

Create a new migration based on database changes

bin/console make:migration

List all configured routes in application

bin/console debug:router

Display environment variables used in the container

bin/console debug:container --env-vars

Dump the default configuration for security

bin/console config:dump-reference security