PHP errors are messages shown by the PHP engine when something goes wrong in your code. These errors tell you what the problem is and where it happened in your file. They help developers find and fix mistakes like missing semicolons, undefined variables, or wrong function names.
In simple words — a PHP error means your code has a problem that stops it from running properly or gives unexpected results.
If you are learning PHP or building your first web application, you have probably seen scary messages like:
Parse error: syntax error, unexpected '}' in index.php on line 12
Don’t worry — it happens to every developer.
PHP errors are common and usually easy to fix once you understand what they mean.
In this guide, we’ll cover the most common PHP errors, explain why they happen, and show you simple ways to fix them.
By the end, you’ll be able to debug your PHP code confidently and save hours of frustration.
1. Parse Error (Syntax Error)
What It Means
A parse error happens when PHP cannot understand your code because of a syntax mistake — like a missing semicolon, unclosed quote, or misplaced bracket.
Example
<?php
echo "Hello World"
?>
🚫 Error Message
Parse error: syntax error, unexpected end of file in index.php on line 2
✅ How to Fix
You forgot a semicolon (;
) at the end of the statement. Fix it like this:
<?php
echo "Hello World";
?>
🔍 Tip
Always check:
- Missing semicolons (
;
) - Unmatched parentheses
()
, braces{}
, or brackets[]
- Unclosed quotes (
"
,'
)
Using an editor like VS Code or PhpStorm can highlight these issues automatically.
2. Undefined Variable
What It Means
This error appears when you use a variable that was never declared or initialized.
Example
<?php
echo $name;
?>
🚫 Error Message
Notice: Undefined variable: name in index.php on line 2
✅ How to Fix
Always initialize your variables before using them.
<?php
$name = "John";
echo $name;
?>
🔍 Tip
You can use the isset()
or empty()
function to check if a variable exists before using it:
if (isset($name)) {
echo $name;
}
3. Undefined Index
What It Means
You’re trying to access an array element or a form field that doesn’t exist.
Example
<?php
echo $_POST['email'];
?>
If the form doesn’t contain an input named “email,” PHP will show an undefined index warning.
🚫 Error Message
Notice: Undefined index: email in form.php on line 2
✅ How to Fix
Use isset()
to check whether the index exists:
if (isset($_POST['email'])) {
echo $_POST['email'];
} else {
echo "Email not provided";
}
4. Undefined Function
What It Means
This error means you’re calling a function that doesn’t exist or has a typo.
Example
<?php
ech("Hello");
?>
🚫 Error Message
Fatal error: Uncaught Error: Call to undefined function ech() in index.php on line 2
✅ How to Fix
Check for typos or ensure the function is defined before calling it.
<?php
echo("Hello");
?>
If you’re calling a custom function, make sure the file containing it is included:
include 'functions.php';
myFunction();
5. Fatal Error: Call to Undefined Method
What It Means
You’re calling a method that doesn’t exist in a class or object.
Example
class User {
public function login() {
echo "User logged in";
}
}
$obj = new User();
$obj->logout();
🚫 Error Message
Fatal error: Uncaught Error: Call to undefined method User::logout()
✅ How to Fix
Check the class and make sure the method exists or that you spelled it correctly.
$obj->login();
6. Missing File or Include Error
What It Means
This error occurs when PHP can’t find the file you’re trying to include or require.
Example
<?php
include('header.php');
?>
If header.php
doesn’t exist or the path is wrong:
🚫 Error Message
Warning: include(header.php): failed to open stream: No such file or directory
✅ How to Fix
Check your file path:
- Is the file name correct (case-sensitive)?
- Is the path relative to the current file?
Example:
include 'includes/header.php';
If the file is essential for running the app, use require
instead of include
.
7. Division by Zero
What It Means
PHP cannot divide a number by zero — it’s mathematically invalid.
Example
<?php
$num = 10;
$div = 0;
echo $num / $div;
?>
🚫 Error Message
Warning: Division by zero in math.php on line 3
✅ How to Fix
Check the denominator before dividing:
if ($div != 0) {
echo $num / $div;
} else {
echo "Cannot divide by zero!";
}
8. Headers Already Sent
What It Means
You’re trying to send HTTP headers (like header()
or setcookie()
) after PHP has already sent output to the browser.
Example
<?php
echo "Welcome!";
header("Location: home.php");
?>
🚫 Error Message
Warning: Cannot modify header information - headers already sent by (output started at index.php:2)
✅ How to Fix
Always call header()
or setcookie()
before any HTML or echo
output:
<?php
header("Location: home.php");
exit;
?>
Or use output buffering:
<?php
ob_start();
echo "Hello";
header("Location: home.php");
ob_end_flush();
?>
9. Maximum Execution Time Exceeded
What It Means
Your PHP script took too long to run. This usually happens with infinite loops or large data operations.
Example
<?php
while (true) {
echo "Running...";
}
?>
🚫 Error Message
Fatal error: Maximum execution time of 30 seconds exceeded in script.php
✅ How to Fix
- Fix the logic to avoid infinite loops.
- Increase the execution time (only if needed):
ini_set('max_execution_time', 60); // 60 seconds
10. Memory Limit Exhausted
What It Means
Your script used more memory than PHP allows (default 128MB or 256MB).
Example
<?php
$bigArray = range(1, 100000000);
?>
🚫 Error Message
Fatal error: Allowed memory size of 134217728 bytes exhausted
✅ How to Fix
- Optimize your code (avoid loading huge arrays).
- Increase the limit temporarily:
ini_set('memory_limit', '512M');
11. Object Not Found / Null Object
What It Means
You’re trying to access an object property or method on a variable that is null or not an object.
Example
<?php
$user = null;
echo $user->name;
?>
🚫 Error Message
Fatal error: Uncaught Error: Attempt to read property "name" on null
✅ How to Fix
Check if the object is valid before accessing it:
if (is_object($user)) {
echo $user->name;
}
Or use PHP 8’s null-safe operator:
echo $user?->name;
12. SQL Injection or Query Errors
What It Means
Your SQL query failed, often because of missing quotes, wrong syntax, or unsafe user input.
Example
<?php
mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
?>
If $id
contains text, this fails.
✅ How to Fix
Always use prepared statements:
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
Bonus Tips: How to Debug PHP Errors Like a Pro
- Turn on Error Reporting (for Development Only)
Add this at the top of your file:
ini_set('display_errors', 1);
error_reporting(E_ALL);
- Check Your Error Log File
In yourphp.ini
file, find the path:
error_log = /var/log/php_errors.log
- Use
var_dump()
andprint_r()
To inspect variables and arrays while debugging:
var_dump($data);
- Use Xdebug for Advanced Debugging
Xdebug helps you trace errors, performance, and step through code in VS Code or PhpStorm. 🏁 Conclusion
PHP errors are not your enemies — they are your best teachers.
Every warning or notice points directly to what went wrong and where to fix it.
By understanding the most common PHP errors — from syntax mistakes to undefined variables and header issues — you can easily build cleaner, more stable, and professional web applications.
Remember:
- Always enable error reporting while developing.
- Test your code step-by-step.
- Use IDEs that highlight errors automatically.
Once you master debugging, you’ll find PHP development faster, easier, and far more enjoyable!