PHP & MySQL

Common PHP Errors & Solutions

Reference guide to common PHP errors - syntax errors, undefined variables, type errors, and database failures. Includes debugging tips for each.

Error Reporting Setup

<?php
// Development - show all errors
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Production - log errors, don't display
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', '/var/log/php_errors.log');

Parse Error (Syntax Error)

// Parse error: syntax error, unexpected 'echo'
<?php
$name = 'Alice'  // Missing semicolon!
echo $name;

// FIX:
$name = 'Alice';
echo $name;

// Common causes:
// - Missing semicolons
// - Unclosed brackets, parentheses, quotes
// - Missing opening/closing PHP tags

Undefined Variable

// Warning: Undefined variable $username
echo $username;

// FIX 1: Initialize the variable
$username = '';
echo $username;

// FIX 2: Use null coalescing
echo $username ?? 'Guest';

// FIX 3: Check with isset()
if (isset($username)) {
    echo $username;
}

Undefined Array Key

// Warning: Undefined array key "email"
$email = $_POST['email'];

// FIX: Use null coalescing or isset()
$email = $_POST['email'] ?? '';
$email = isset($_POST['email']) ? $_POST['email'] : '';

// For nested arrays:
$city = $_POST['address']['city'] ?? 'Unknown';

Cannot Modify Header Information

// Warning: Cannot modify header information - headers already sent
echo 'Hello';
header('Location: /dashboard');  // ERROR - output already sent

// FIX: Call header() BEFORE any output
// No echo, print, HTML, or whitespace before <?php
header('Location: /dashboard');
exit;

// Also check for BOM (byte order mark) in files
// and whitespace before <?php

Database Connection Errors

// Fatal: Uncaught PDOException: SQLSTATE[HY000] [2002]
// Connection refused

// FIX: Check credentials and use try-catch
try {
    $pdo = new PDO(
        'mysql:host=localhost;dbname=mydb;charset=utf8mb4',
        'username',
        'password',
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
} catch (PDOException $e) {
    error_log('DB Connection failed: ' . $e->getMessage());
    die('Database unavailable. Please try later.');
}

// Common causes:
// - Wrong host/port (try 127.0.0.1 instead of localhost)
// - MySQL service not running
// - Wrong credentials
// - Database doesn't exist

Type Errors (PHP 8+)

// TypeError: strlen(): Argument #1 must be type string, null given
$length = strlen(null);

// FIX: Check or cast type
$length = strlen($value ?? '');
$length = strlen((string) $value);

// TypeError: Cannot pass argument of type string to int parameter
function add(int $a, int $b): int { return $a + $b; }
add('5', '3');  // TypeError in strict mode

// FIX: Cast input
add((int) '5', (int) '3');

Quick Reference

ErrorCommon Fix
Unexpected end of fileMissing closing brace }
Call to undefined functionCheck spelling, missing require/include
Class not foundCheck namespace, autoloader, require
Maximum execution time exceededInfinite loop or heavy query; set_time_limit()
Allowed memory size exhaustedMemory leak; increase memory_limit or optimize
Permission deniedFile/directory permissions (chmod)

Last updated: 2026 • Browse all courses