Deprecated or incorrect usage of functions
A lot of older PHP functions and constants are effectively deprecated or not recommended for use, and it's not always obvious that this is the case or what the replacements are.
FILTER_SANITIZE_STRING (for the filter_var, filter_input methods) is deprecated, and instead you should use htmlspecialchars() instead of the filter functions when filtering standard strings. Various other FILTER constants are also deprecated - check the page in the link to see which ones are currently still being used.
PHP 8
As of PHP version 8, the standard error reporting setting in php.ini has changed:
# Old default, in PHP 7 and earlier
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
# New default
error_reporting = E_ALL
You may therefore get a lot of warning messages in your Apache logs if you have some deprecated code or bad code lying around, unlike undefined array keys.
Undefined variables and array keys
Check the variable actually exists before trying to evaluate it; otherwise, you will get a lot of undefined variable/array key warnings in your logs.
// This line will cause an undefined array key warning
// if the key does not exist in the array, or if the
// array itself is not defined
if ($array['some_key']) {
}
Instead, use the isset function. The array_key_exists function does not help here (it will still throw warnings if the array key didn't exist).
if (isset($array['some_key'])) {
$myVariable = $array['some_key'];
}
In situations where you just want to check if a variable exists, and if not use some other value, you can use another feature of PHP 8: the null coalesce operator, which also will not throw up undefined variable warnings.
// The ?? operator sets $insult to the first non-null expression.
// If $yourMom is not null, $insult = $yourMom, otherwise it equals "fat".
$insult = $yourMom ?? "fat";