Every PHP developer, no matter how experienced, has faced a wall of red error text at some point. For beginners, these errors can feel confusing and discouraging, but most of them come down to a small handful of recurring mistakes. Once you learn to recognize the pattern behind each error, fixing it becomes second nature.
In this guide, you will walk through the 10 most common PHP errors beginners make and exactly how to resolve each one.
1. Missing Semicolon
The Error:
Parse error: syntax error, unexpected token, expecting ";"Why It Happens:
PHP requires a semicolon at the end of every statement. Forgetting one is one of the most common beginner mistakes.
// Wrong
$name = "Marshall"
echo $name;
// Correct
$name = "Marshall";
echo $name;How to Fix It:
Always check the line just above the one mentioned in the error message, since PHP often reports the error on the line after the missing semicolon.
2. Undefined Variable
The Error:
Warning: Undefined variable $usernameWhy It Happens:
This happens when a variable is used before it has been assigned a value, often due to a typo or a variable defined inside a conditional block that never runs.
// Wrong
if ($isLoggedIn) {
$username = "John";
}
echo $username;
// Correct
$username = "";
if ($isLoggedIn) {
$username = "John";
}
echo $username;How to Fix It:
Initialize variables before using them, and use isset() to check whether a variable exists before referencing it.
3. Undefined Array Key
The Error:
Warning: Undefined array key "email"Why It Happens:
This occurs when trying to access an array index or key that has not been set, commonly seen with $_POST or $_GET data.
// Wrong
$email = $_POST['email'];
// Correct
$email = $_POST['email'] ?? '';How to Fix It:
Use the null coalescing operator (??) or isset() to safely handle missing keys, especially with form data.
4. Call to Undefined Function
The Error:
Fatal error: Uncaught Error: Call to undefined functionWhy It Happens:
This usually means the function name is misspelled, the file containing the function was never included, or you are using a function from a PHP extension that is not enabled.
// Wrong
echo strtouppercase("hello");
// Correct
echo strtoupper("hello");How to Fix It:
Double check the spelling against the official PHP documentation, and confirm any required file is properly included with require or include.
5. Headers Already Sent
The Error:
Warning: Cannot modify header information, headers already sent by...Why It Happens:
This happens when header() is called after any output, including plain HTML, whitespace, or an accidental blank line before the opening <?php tag.
// Wrong
?>
<?php
header("Location: dashboard.php");
// Correct
<?php
header("Location: dashboard.php");
exit;
?>How to Fix It:
Make sure nothing is printed to the browser before calling header(), and consider using ob_start() at the top of the script to buffer output.
6. Class Not Found
The Error:
Fatal error: Uncaught Error: Class "Database" not foundWhy It Happens:
This means PHP cannot locate the class definition, usually because the file was never included or an autoloader is not set up correctly.
// Wrong
$db = new Database();
// Correct
require_once 'Database.php';
$db = new Database();How to Fix It:
Include the file that defines the class before instantiating it, or set up Composer's autoloader if you are working with namespaced classes.
7. MySQLi Connection Failure
The Error:
Fatal error: Uncaught mysqli_sql_exception: Access denied for userWhy It Happens:
This typically means incorrect database credentials, a MySQL service that is not running, or a database name that does not exist.
// Wrong
$conn = mysqli_connect("localhost", "root", "wrongpass", "mydb");
// Correct
$conn = mysqli_connect("localhost", "root", "", "mydb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}How to Fix It:
Verify your username, password, host and database name, and confirm the MySQL service is actively running in your local server panel.
8. Trying to Access Array Offset on Value of Type Null
The Error:
Warning: Trying to access array offset on value of type nullWhy It Happens:
This occurs when a database query returns no results, and the code tries to access a key on that empty or null result.
// Wrong
$result = mysqli_fetch_assoc($query);
echo $result['name'];
// Correct
$result = mysqli_fetch_assoc($query);
if ($result) {
echo $result['name'];
} else {
echo "No record found";
}How to Fix It:
Always check that a query returned a result before trying to read data from it.
9. Division by Zero
The Error:
Warning: Division by zeroWhy It Happens:
This happens when a calculation attempts to divide by a variable that holds a value of zero, often due to unvalidated user input.
// Wrong
$average = $total / $count;
// Correct
$average = ($count > 0) ? $total / $count : 0;How to Fix It:
Validate denominators before performing any division, and provide a fallback value when the denominator could be zero.
10. Memory Exhausted Error
The Error:
Fatal error: Allowed memory size of 134217728 bytes exhaustedWhy It Happens:
This usually means a script is processing a very large dataset, stuck in an infinite loop, or loading more data into memory than the server allows.
// Wrong
while (true) {
$data[] = fetchMoreData();
}
// Correct
foreach ($dataset as $item) {
processItem($item);
unset($item);
}How to Fix It:
Review loops for proper exit conditions, process large datasets in smaller batches, and increase the memory_limit value in php.ini only as a last resort.
Final Thoughts
Almost every PHP error follows a predictable pattern once you know what to look for. Reading the error message carefully, checking the exact line it points to, and understanding what PHP expected versus what it received will resolve the majority of beginner mistakes.
The more errors you encounter and fix, the faster you will start recognizing them on sight, which is one of the clearest signs of growth as a developer.
Hit me with a comment!