PHP: How do you redirect to another page?
A redirect in PHP is one line of code. There are two mistakes around it that happen so often, though, that they make up the larger part of this article.
PHP: How do you remove empty elements from an array?
array_filter($array) is the answer you find everywhere. It is short, it works
— and it removes more than most people expect. The number zero, among other
things.
PHP: How do you return JSON?
Two lines and you are done. Almost — because json_encode() can fail, and if
you do not check for that you send an empty body with status 200. The client
then sees no error, just nothing.
PHP: How do you sort an array by a column?
Sorting records by a column is one line. The older variant with the minus sign, which you still find in many answers, fails on exactly the data type people sort most often: prices.
PHP: How do you use enums?
Status values, priorities, traffic light colours: again and again you need a fixed list of permitted values. Since PHP 8.1 there is a language construct for that, so you no longer have to make do with class constants.
PHP: What do "Undefined variable" and "Undefined array key" mean?
These three messages are probably the most common ones in PHP altogether. They always mean the same thing: you are accessing something that does not exist. More important than making the message go away is the question of why it does not exist.
PHP: What does "Cannot modify header information" mean?
Warning: Cannot modify header information - headers already sent by
(output started at /var/www/html/datei.php:1) in /var/www/html/datei.php on line 3
This message tells you the cause quite precisely. You just have to know which of the two line numbers in it is the right one — and most people read the wrong one.
PHP: What does this symbol mean?
Searching for a special character is awkward. That is exactly why, in somebody else’s PHP code, you keep running into operators you cannot place. This article goes through the ones where that happens most often.
PHP: What is stdClass?
stdClass is an empty class. No properties, no methods, no constructor. You
usually meet it without having asked for it — as the return value of
json_decode(). And then you have a few questions.
PHP: What is the difference between public, private and protected?
You find the table for this in every tutorial, and it is correct. But it
suggests one thing that is wrong: that private applies to the individual
object. It does not, and that is a good thing.