Updated: 06 May 2024
PHP
PHP Security Advisories Database
Updated: 22 December 2022
PHP in Docker
Updated: 14 March 2024
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 file permissions
Updated: 06 May 2024
Set folder and file permissions for a php project
find . -type d -exec chmod 755 {} \; find . -type f -exec chmod 644 {} \;
Coding standards
Updated: 09 June 2023
PHP_CodeSniffer
Two scripts: phpcs and phpcbf
https://github.com/squizlabs/PHP_CodeSniffer
A set of rules for Squizlabs PHP_CodeSniffer
https://github.com/WordPress/WordPress-Coding-Standards
PHP Coding Standards Fixer
https://cs.symfony.com/
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer
Homestead Vagrant xdebug3 vscode phpspec
Updated: 18 July 2021
Reset to original state
./vagrant destroy --force && ./vagrant up
.vscode/launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003, "log": true, "pathMappings": { "/home/vagrant/homestead.test": "/home/chris/code/homestead.test" } } ] }
Start an ssh session
./vagrant ssh
Create the xdebug log file
sudo touch /var/log/xdebug.log && sudo chmod 777 /var/log/xdebug.log
Create xdebug settings
sudo nano /etc/php/7.4/fpm/conf.d/20-xdebug.ini
zend_extension=xdebug.so xdebug.mode = debug xdebug.discover_client_host = true xdebug.client_host = 192.168.10.1 xdebug.client_port = 9003 xdebug.max_nesting_level = 512 ; required for cli debugging but for browser ; could omit and use query string at end ; of url instead XDEBUG_SESSION_START=name xdebug.start_with_request = yes ; ensure file exists and is writeable xdebug.log = /var/log/xdebug.log
Switch to cli PHP 7.4
php74
For cli debugging (alias xon=’sudo phpenmod -s cli xdebug’)
xon
After these changes
./vagrant reload --provision
Run phpspec
./vagrant ssh tail -f /var/log/xdebug.log project/dir vendor/bin/phpspec run
If vscode will not stop on a breakpoint try re-enabling PHP Debug extension and restarting vscode.
Run ngrok (outside of Homestead) pointing at a configured Homestead site
ngrok http 192.168.10.10:80 -host-header=site.dev
Reporting deprecations, notices, warnings and errors
Updated: 06 May 2024
Turn on reporting
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 your settings
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);
PHP
Updated: 14 May 2024
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
Date and time
Updated: 23 February 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: 17 May 2023
List all configured routes in application
bin/console debug:router
Display environment variables used in the container
bin/console debug:container --env-vars