Example 1 — Print File and Line
Catch an Exception and show where it was thrown using getFile() and getLine().
<?php
try {
throw new Exception("Demo failure");
} catch (Exception $e) {
echo "Has .php: ", str_ends_with($e->getFile(), ".php") ? "yes" : "no", PHP_EOL;
echo "Line: ", $e->getLine(), PHP_EOL;
echo $e->getMessage(), PHP_EOL;
} How It Works
- throw creates the Exception on a specific line of this script.
- getFile() returns the full path of that script.
- str_ends_with() confirms the path points at a . php file.
- getLine() returns the throw line number.

