Errors

View saved

See errors while developing

Turn on reporting locally so mistakes are visible.

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);

Throw exceptions

Modern PHP code often throws exceptions for failures.

function find(int $id): string {
  if ($id <= 0) {
    throw new InvalidArgumentException("id must be positive");
  }
  return "ok";
}

try {
  echo find(0);
} catch (InvalidArgumentException $e) {
  echo $e->getMessage(), "\n";
}

Log instead of showing on production

Public sites should log errors, not print stack traces to visitors.

Error habits

  • Distinguish notices, warnings, and fatals while learning
  • Use try/catch around expected failure points
  • Validate input early
  • Tools like Xdebug help step through hard bugs

Comments

One comment per signed-in account. Comments are saved with this page’s URL.