Skip to content

Debugging

By default, PHP may hide errors. Enable them during development:

<?php
error_reporting(E_ALL); // Report all errors
ini_set('display_errors', 1); // Show errors on screen

Place this at the top of your script or in a config file.


Shows the type and value of a variable. Best for debugging.

$name = "John";
$age = 25;
$prices = [10, 20, 30];
var_dump($name);
// string(4) "John"
var_dump($age);
// int(25)
var_dump($prices);
// array(3) { [0]=> int(10) [1]=> int(20) [2]=> int(30) }

Displays arrays and objects in a human-readable format. Easier to read than var_dump() but shows less detail.

$user = [
'name' => 'John',
'email' => 'john@example.com',
'roles' => ['admin', 'editor']
];
print_r($user);
// Array
// (
// [name] => John
// [email] => john@example.com
// [roles] => Array
// (
// [0] => admin
// [1] => editor
// )
// )
echo '<pre>';
print_r($user);
echo '</pre>';

A common pattern for quick debugging:

echo '<pre>';
var_dump($variable);
echo '</pre>';
die(); // Stop execution here

Or as a one-liner:

die(var_dump($variable));

ConstantDescription
E_ALLAll errors and warnings
E_ERRORFatal runtime errors
E_WARNINGRuntime warnings
E_NOTICERuntime notices
// Show all errors except notices
error_reporting(E_ALL & ~E_NOTICE);
// Show only errors and warnings
error_reporting(E_ERROR | E_WARNING);

  • Always disable error display in production use logging instead
  • Use var_dump() over echo for debugging, it shows the type
  • Check your PHP error log for errors that don’t display on screen
  • Use exit or die() to stop execution at a specific point