Skip to content

PHP Tutorial Error Handling

Warnings, Notices and Deprecated Messages

Lesson 187 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Warnings are the messages you will see most while learning. Each has a standard cause and a standard fix.

Undefined variable

index.php
PHP
<?php
echo $total;
Output
OUTPUT
Warning: Undefined variable $total in /path/to/index.php on line 2

Cause: you used a variable before giving it a value, or you mistyped its name. Fix: set the variable first, check the spelling, or use $total ?? 0.

Undefined array key

index.php
PHP
<?php
$user = ["name" => "Ana"];

echo $user["email"];
Output
OUTPUT
Warning: Undefined array key "email" in /path/to/index.php on line 4

Cause: the key does not exist. Fix: $user["email"] ?? "", or check with isset() or array_key_exists().

Array to string conversion

index.php
PHP
<?php
$items = ["a", "b"];

echo "Items: " . $items;
Output
OUTPUT
Warning: Array to string conversion in /path/to/index.php on line 4
Items: Array

Cause: you printed an array as if it were text. Fix: implode(", ", $items) or loop through it.

Trying to read a property of null

index.php
PHP
<?php
$user = null;

echo $user->name;
Output
OUTPUT
Warning: Attempt to read property "name" on null in /path/to/index.php on line 4

Cause: a value you expected to be an object is null (for example, nothing was found). Fix: check for null, or use the nullsafe operator: $user?->name.

A non-numeric value

index.php
PHP
<?php
echo "5 apples" + 3;
Output
OUTPUT
Warning: A non-numeric value encountered in /path/to/index.php on line 2
8

Fix: validate input before using it in maths.

Headers already sent

index.php
PHP
<?php
echo "Hello";
header("Location: /home");
Output
OUTPUT
Hello
Warning: Cannot modify header information - headers already sent by (output started at /path/to/index.php:2) in /path/to/index.php on line 3

Cause: header(), setcookie() or session_start() was called after output. Fix: move them before any output. Look for stray spaces before <?php too.

Deprecated

A message like Deprecated: ... is deprecated means a function or a way of writing will disappear. Examples are passing null to a function that expects a string (deprecated in PHP 8.1), or using ${var} inside strings (PHP 8.2).

index.php
PHP
<?php
echo strlen(null);
Output
OUTPUT
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /path/to/index.php on line 2
0

Fix: pass a real string, or handle the null first.

How to deal with them

  1. Read the message and the line number.
  2. Look at that line and the one before it.
  3. Fix the cause, do not hide the message.

The @ operator hides errors: avoid it

@ in front of an expression silences its warnings. It hides the problem and makes bugs hard to find.

index.php
PHP
<?php
$value = @$undefined["x"];   // hides the warning, the bug remains
var_dump($value);
Output
OUTPUT
NULL

Use ?? or a proper check instead.

info
Reading a message
PHP error messages are written in plain English. Read the whole sentence before you search for it online.