PHP Idioms
This document is intended to capture PHP changes that some developers may find unclear because they differ from what was done in the past. Some but not all are driven by PHP8 changes.
Identical comparison operator
The comparison operator ===
means “equal to and of the same type.” It has become more important in PHP8+, which does stricter comparisons.
See the PHP documentation on Comparison Operators for more details.
Null coalescing operator
This null coalescing operator ??
checks that the first operand exists and is not null, i.e. isset
. If so, it returns the first operand; otherwise, it returns the second operand.
So
$enddate = zen_db_input($_REQUEST['end_date'] ?? date('Y-m-d'));
is the same as:
if (isset($_REQUEST['end_date'])) {
$enddate = zen_db_input($_REQUEST['end_date']);
} else {
$enddate = zen_db_input(date('Y-m-d'));
}
See the PHP documentation on Null coalescing operator for more details.
Missing PHP close tag ?>
at the end of a file
The close tag ?>
is not recommended at the end of a file, and should be left off.
Coding Help:
- PHP Errors, Warnings and Deprecated messages after upgrading
- PHP Idioms
- Release Specific Upgrade Considerations
- Upgrading plugins to work with 1.5.8/PHP 8.0+
… and when all else fails:
- PHP migration guides in the php.net documentation