Miscellaneous
Basic error handling
try {
// Do something
} catch (Exception $e) {
// Handle exception
} finally {
echo "Always print!";
}
Exception in PHP 8.0
$nullableValue = null;
try {
$value = $nullableValue ?? throw new InvalidArgumentException();
} catch (InvalidArgumentException) { // Variable is optional
// Handle my exception
echo "print me!";
}
Custom exception
Usage
try {
$condition = true;
if ($condition) {
throw new MyException('bala');
}
} catch (MyException $e) {
// Handle my exception
}
Nullsafe Operator
// As of PHP 8.0.0, this line:
$result = $repo?->getUser(5)?->name;
// Equivalent to the following code:
if (is_null($repo)) {
$result = null;
} else {
$user = $repository->getUser(5);
if (is_null($user)) {
$result = null;
} else {
$result = $user->name;
}
}
See also: Nullsafe Operator
Regular expressions
See: Regex in PHP
fopen() mode
| - | - |
|---|---|
r |
Read |
r+ |
Read and write, prepend |
w |
Write, truncate |
w+ |
Read and write, truncate |
a |
Write, append |
a+ |
Read and write, append |
Runtime defined Constants
define("CURRENT_DATE", date('Y-m-d'));
// One possible representation
echo CURRENT_DATE; # => 2021-01-05
# => CURRENT_DATE is: 2021-01-05
echo 'CURRENT_DATE is: ' . CURRENT_DATE;