Shebang
#!/usr/bin/php <?php
Change working directory to script directory
$workDir = realpath(dirname(__FILE__)); chdir($workDir);
Arguments
$_SERVER['argv'][0] // = scriptname (myscript.php) $_SERVER['argv'][1] // = first cmd argument "foo" (myscript.php foo)
Get Environment Variable
$certbotDomain = getenv('CERTBOT_DOMAIN');
Write to stderr
fwrite(STDERR, “hello, world\n”);
Exit code
exit(1);
Execute a shell command and return the output
$output = shell_exec('uname -r');
Disable xdebug html output
ini_set('html_errors', false);
Color output
Colors: https://misc.flogisoft.com/bash/tip_colors_and_formatting
echo "\033[31mERROR: $message \033[33mLine $lineNumber \033[39m$line\n";
Background color:
echo "\033[31;43mRed on yellow background\n";
Reset colors to defaults:
echo "\033[0m";
Direct inline execution
php -r '$parts = explode(".", phpversion()); echo $parts[0] . "." . $parts[1];'