25 Common PHP Errors and How to Fix Them

25 Common PHP Errors and How to Fix Them

Every PHP developer encounters errors while coding. Learning how to identify and fix these errors quickly can save hours of debugging.

This guide covers the most common PHP errors, explains why they occur, and shows how to fix them with examples.


1. Parse Error: Syntax Error

Error

Parse error: syntax error, unexpected ';'

Cause

Missing or extra characters like ;, {, }, or quotes.

Example

<?php
echo "Hello"
?>

Fix

<?php
echo "Hello";
?>

2. Undefined Variable

Error

Notice: Undefined variable

Cause

Using a variable before assigning a value.

echo $name;

Fix

$name = "Sunil";
echo $name;

3. Undefined Index

Error

Undefined array key

Cause

Accessing an array element that doesn't exist.

Fix

if(isset($_POST["email"])){
    echo $_POST["email"];
}

4. Call to Undefined Function

Error

Fatal error: Call to undefined function

Cause

Misspelled function name or missing extension.

Fix

Check the spelling and ensure the required PHP extension is enabled.


5. Cannot Modify Header Information

Error

Headers already sent

Cause

Output is sent before header().

Wrong

echo "Welcome";
header("Location: home.php");

Correct

header("Location: home.php");
exit;

6. Division by Zero

Wrong

echo 100 / 0;

Fix

if($number != 0){
    echo 100 / $number;
}

7. Database Connection Failed

Cause

Wrong database credentials.

Fix

Verify:

  • Host

  • Username

  • Password

  • Database Name


8. SQL Syntax Error

Always print the SQL query and verify it before execution.


9. Session Not Working

Always start sessions before any HTML output.

session_start();

10. File Upload Not Working

Check:

  • Form method is POST

  • enctype="multipart/form-data"

  • Upload folder permissions

  • PHP upload limits


11. White Screen of Death

Enable error reporting.

error_reporting(E_ALL);
ini_set('display_errors',1);

12. 404 Page Not Found

Check:

  • File name

  • URL

  • Rewrite rules

  • Server configuration


13. Include File Failed

Wrong

include "config.php";

If the file is in another folder:

include "includes/config.php";

14. Undefined Function mysqli_connect()

The MySQLi extension is disabled.

Enable it in your PHP installation.


15. Maximum Execution Time Exceeded

Increase:

max_execution_time

Or optimize your code.


16. Allowed Memory Size Exhausted

Increase:

memory_limit = 256M

17. Password Verification Failed

Always use:

password_hash()

and

password_verify()

18. JSON Decode Returns Null

Always check:

json_last_error_msg();

19. Timezone Warning

Set the timezone.

date_default_timezone_set("Asia/Kolkata");

20. Invalid Argument for foreach()

Wrong

foreach($users as $user)

When $users isn't an array.

Fix

if(is_array($users)){
    foreach($users as $user){
    }
}

21. Missing Semicolon

One of the most common beginner mistakes.

Always end PHP statements with:

;

22. Wrong File Permissions

If PHP cannot write files:

Linux example:

chmod 755 uploads

23. Email Not Sending

Check:

  • SMTP configuration

  • Mail server

  • Firewall

  • Use PHPMailer instead of the native mail() function.


24. Invalid JSON Response

When building APIs:

header("Content-Type: application/json");

Return valid JSON.


25. Blank Page After Deployment

Check:

  • PHP version

  • File permissions

  • Error logs

  • Missing extensions

  • Database credentials


Debugging Tips Every PHP Developer Should Know

  • Enable error reporting during development.

  • Read the complete error message before changing code.

  • Use var_dump() and print_r() to inspect variables.

  • Log errors to a file instead of displaying them in production.

  • Test your application in a local environment before deployment.

  • Keep PHP and dependencies updated.


Comments

Popular posts from this blog

Simple PHP Mysql Shopping Cart

How to seperate character from string in php

How to Delete record using PHP Ajax