Error reporting
// development
error_reporting(E_ALL);
ini_set('display_errors', '1');
// production: log, don't show
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/../logs/php-error.log');| Level | Meaning |
|---|---|
E_ERROR | fatal error, script stops |
E_WARNING | problem, script continues |
E_NOTICE | minor issue |
E_DEPRECATED | feature will be removed |
E_ALL | everything |
Exceptions
function divide(int $a, int $b): float {
if ($b === 0) {
throw new InvalidArgumentException('Division by zero');
}
return $a / $b;
}
try {
echo divide(10, 0);
} catch (InvalidArgumentException $e) {
echo 'Input error: ' . $e->getMessage();
} catch (Throwable $e) { // catches everything (Exception + Error)
error_log($e);
} finally {
// always runs (close files, connections …)
}| Method | Returns |
|---|---|
$e->getMessage() | error text |
$e->getCode() | error code |
$e->getFile() / $e->getLine() | where it was thrown |
$e->getTraceAsString() | call stack |
$e->getPrevious() | wrapped exception |
Common built-in classes: Exception, RuntimeException, InvalidArgumentException, LogicException, TypeError, ValueError, JsonException, PDOException.
class PaymentException extends RuntimeException {} // own exception type
set_exception_handler(function (Throwable $e) { // last line of defence
error_log($e);
http_response_code(500);
echo 'Something went wrong.';
});Logging and debugging
| Tool | Use |
|---|---|
var_dump($x) | type and value |
print_r($x, true) | readable string of arrays/objects |
error_log("msg") | write to the error log |
debug_print_backtrace() | show how you got here |
memory_get_usage() / memory_get_peak_usage() | memory in bytes |
microtime(true) | measure run time |
| Xdebug extension | step debugging in the IDE |
$start = microtime(true);
// … code …
printf("%.3f s\n", microtime(true) - $start);JSON
$data = ['name' => 'Anna', 'tags' => ['php', 'äöü']];
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
$arr = json_decode($json, true, 512, JSON_THROW_ON_ERROR); // true = associative array
$obj = json_decode($json); // stdClass object: $obj->name
json_validate($json); // PHP 8.3: check without decodingWithout JSON_THROW_ON_ERROR, check json_last_error() !== JSON_ERROR_NONE and json_last_error_msg().
Simple JSON API response:
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok' => true, 'items' => $items], JSON_THROW_ON_ERROR);
exit;PHP on the command line
| Command | Does |
|---|---|
php -v | version |
php -m | loaded extensions |
php -i | full configuration (like phpinfo()) |
php --ini | which php.ini files are used |
php -l file.php | syntax check (lint) |
php script.php arg1 | run a script; args in $argv, count in $argc |
php -r 'echo 1+1;' | run code directly |
php -a | interactive shell |
php -S localhost:8000 -t public | built-in dev web server (not for production) |
Composer (package manager)
| Command | Does |
|---|---|
composer init | create composer.json |
composer require vendor/package | add a package |
composer require --dev phpunit/phpunit | add a dev-only package |
composer install | install from composer.lock |
composer update | update packages within version limits |
composer dump-autoload | rebuild the autoloader |
Load everything with require __DIR__ . '/vendor/autoload.php';
For server-side commands (services, logs, Docker) see Linux & Docker Command Cheat Sheet.
Previous: PHP Databases with PDO · Start: PHP Basics: Syntax, Variables and Output
Put together with AI as a quick reference I keep for my own work. Entries like this are usually not reviewed in detail by a person.