Every programmer has encountered a blank white screen or an error they didn't understand. PHP is a friendly language, but it has its pitfalls. Here are five common mistakes and their solutions.
1. Missing Semicolon ( ; )
Every statement in PHP must end with a semicolon. If forgotten, the server will report a Parse error.
// Incorrect
echo "Hello world"
$name = "Borut"
// Correct
echo "Hello world";
$name = "Borut";
2. Mixing "=" and "=="
A single equals sign (=) is used for assignment, while double (==) is used for comparison. This is a dangerous mistake because the code often runs but behaves incorrectly.
// Incorrect (condition will always be true)
if ($x = 10) { ... }
// Correct (checking if $x equals 10)
if ($x == 10) { ... }
3. "Headers already sent" Error
This happens when you use header() after the server has already sent HTML or even a blank space before the <?php tag.
4. Single vs. Double Quotes
PHP parses variables inside double quotes ("), but treats them as literal text inside single quotes (').
$name = "Borut";
echo 'Hi $name'; // Outputs: Hi $name
echo "Hi $name"; // Outputs: Hi Borut
5. Missing Function Parentheses
Parentheses are required when calling functions; otherwise, PHP might think you are accessing a constant.
// Incorrect
$date = date;
// Correct
$date = date("Y-m-d");