PHP getTraceAsString() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::getTraceAsString() returns the stack trace as a single readable string. Each frame appears on its own line with an index, similar to a printed backtrace.

This is the quickest way to log a stack for humans. If you need to loop over frames in code, use getTrace() instead. The method is final.

Syntax

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

PHP
$text = $e->getTraceAsString();
CallWhat it does
$e->getTraceAsString()Returns the full stack trace as one multiline string.
explode(PHP_EOL, $e->getTraceAsString())Splits the string into individual frame lines.
echo $e->getTraceAsString();Prints the stack directly to output.

getTraceAsString() is final. It is meant for display and logging. For programmatic inspection of frames, call getTrace().

Library declarations
PHP
final public Exception::getTraceAsString(): string

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
stringA multiline stack trace string with numbered frames.

PHP Examples

Example 1 — Print a String Stack Trace

Throw from a helper and echo getTraceAsString() from the catch block.

PHP
<?php
function failing(): never
{
    throw new Exception("String trace demo");
}

try {
    failing();
} catch (Exception $e) {
    echo $e->getTraceAsString(), PHP_EOL;
}

How It Works

  1. failing() throws the Exception.
  2. getTraceAsString() formats the call stack as text.
  3. Frame #0 shows failing().
  4. The exact formatting can include file paths depending on PHP version and context.

Example 2 — Count Lines in the Trace String

Split the string trace and count how many frame lines it has.

PHP
<?php
function a(): never { b(); }
function b(): never { throw new Exception("Deep"); }

try {
    a();
} catch (Exception $e) {
    $lines = array_values(array_filter(explode("\n", $e->getTraceAsString())));
    echo "Line count: ", count($lines), PHP_EOL;
    echo $lines[0], PHP_EOL;
}

How It Works

  1. a() calls b(), which throws.
  2. getTraceAsString() returns one string with newline-separated frames.
  3. explode and array_filter build a clean list of lines.
  4. The first line is the frame closest to the throw.

Example 3 — Include Trace in a Log Line Prefix

Show message first, then a shortened start of the string trace.

PHP
<?php
function task(): never
{
    throw new Exception("Task failed");
}

try {
    task();
} catch (Exception $e) {
    $lines = explode("\n", $e->getTraceAsString());
    echo $e->getMessage(), PHP_EOL;
    echo "Trace starts: ", $lines[0], PHP_EOL;
}

How It Works

  1. getMessage() prints the short reason.
  2. getTraceAsString() holds the full stack.
  3. explode() keeps the first line for a compact log.
  4. Store the full string when you need complete debugging detail.

Frequently Asked Questions

A multiline string version of the exception stack trace, ready to print or store in a log.
getTrace() returns an array you can inspect in code. getTraceAsString() returns formatted text.
No. It is final on Exception.
No for production users. Log it privately and show a generic error message instead.
Yes. __toString() builds a longer summary that also includes the string stack trace.

Next: __toString()

Exception::__toString() returns a string representation of the exception. It usually inclu.

__toString() 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