PHP getLine() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::getLine() returns the line number where the exception object was created. Together with getFile(), it tells you the exact throw location.

Use getLine() in debug output and logs. The value is set automatically when the Exception is constructed. The method is final and returns an integer.

Syntax

Call getLine() with the values you need. Start with this line.

PHP
$line = $e->getLine();
CallWhat it does
$e->getLine()Returns the line number where the exception was created.
$e->getFile() . ":" . $e->getLine()Formats a file:line location.
"Line " . $e->getLine()Builds a short label for display.

getLine() is final. It reports the construction line of the Exception, which is typically the throw line.

Library declarations
PHP
final public Exception::getLine(): int

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
intThe line number in getFile() where the exception was created.

PHP Examples

Example 1 — Show the Throw Line

Throw an Exception and print the line number from getLine().

PHP
<?php
try {
    throw new Exception("Invalid amount");
} catch (Exception $e) {
    echo "Thrown on line ", $e->getLine(), PHP_EOL;
}

How It Works

  1. The throw statement creates the Exception.
  2. PHP stores the current line number on the object.
  3. getLine() returns that integer.
  4. The catch block only reads the value; it does not change it.

Example 2 — Line Inside a Nested Call

Confirm getLine() points at throw inside a nested function, not at the outer call.

PHP
<?php
function inner(): never
{
    throw new Exception("Inner boom");
}

function outer(): never
{
    inner();
}

try {
    outer();
} catch (Exception $e) {
    echo $e->getLine(), PHP_EOL;
    echo $e->getMessage(), PHP_EOL;
}

How It Works

  1. inner() contains the throw.
  2. outer() only calls inner().
  3. getLine() reports the throw line inside inner().
  4. Inspect getTrace() if you need outer()'s frame as well.

Example 3 — Pair getLine() with getFile()

Build a compact location string for a debug echo.

PHP
<?php
try {
    throw new Exception("Cache miss");
} catch (Exception $e) {
    echo "Line ", $e->getLine(), " in ", basename($e->getFile()) !== "" ? "a PHP file" : "unknown", PHP_EOL;
}

How It Works

  1. getFile() provides the script path.
  2. basename() shortens it to a file name.
  3. getLine() returns the throw line.
  4. Together they make a compact debug location.

Frequently Asked Questions

An integer line number in the file returned by getFile(), marking where the exception was created.
It is the creation/throw line, not the line of the catch block.
No. PHP fills file and line when the Exception is constructed.
Yes. Subclasses cannot override it.
Usually no. Show a friendly message to users and keep file and line details in logs for developers.

Next: getTrace()

Exception::getTrace() returns the stack trace as an array of frames. Each frame can includ.

getTrace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful