PHP preg_last_error_msg() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Regex

Definition

In PHP, preg_last_error_msg() returns a short human-readable message for the last PCRE error.

It pairs with preg_last_error(). After a successful preg_* call the message is typically "No error". After a failure you get text such as "Internal error" or a backtrack-limit message.

Syntax

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

PHP
$msg = preg_last_error_msg();
CallWhat it does
preg_last_error_msg()Returns "No error" after a successful preg_* call.
preg_last_error_msg()Returns an error phrase after a failed preg_* call.
preg_last_error() . ': ' . preg_last_error_msg()Combines the numeric code with the message.

This function was added in PHP 8.0. On older PHP versions, map preg_last_error() codes yourself. Prefer fixing patterns over silencing warnings.

Library declarations
PHP
preg_last_error_msg(): string

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
stringA message describing the last PCRE error, or "No error" when the last operation succeeded.

PHP Examples

Example 1 — Message After Success

Run a valid match and print the last error message.

PHP
<?php
preg_match('/cat/', 'a cat');
echo preg_last_error_msg(), PHP_EOL;

How It Works

  1. The pattern /cat/ is valid and finds a match.
  2. No PCRE error is stored.
  3. preg_last_error_msg() returns the text No error.
  4. This is the happy path you want in normal code.

Example 2 — Message After a Broken Pattern

Trigger a failure and read the message instead of only the code.

PHP
<?php
@preg_match('/(/', 'test');
echo preg_last_error_msg(), PHP_EOL;

How It Works

  1. The pattern /(/ cannot compile.
  2. preg_match() fails and stores an error.
  3. preg_last_error_msg() returns a readable phrase such as Internal error.
  4. Show this message in logs when debugging pattern problems.

Example 3 — Code and Message Together

Print both preg_last_error() and preg_last_error_msg() after a bad call.

PHP
<?php
@preg_match('/(/', 'x');
echo preg_last_error(), ': ', preg_last_error_msg(), PHP_EOL;

preg_match('/x/', 'x');
echo preg_last_error(), ': ', preg_last_error_msg(), PHP_EOL;

How It Works

  1. The first block records a failure from the invalid pattern.
  2. You see the numeric code and the message on one line.
  3. A valid call follows and clears the error.
  4. The second line shows 0: No error.

Frequently Asked Questions

It returns a human-readable string for the last PCRE error from a preg_* function, such as "No error" or "Internal error".
preg_last_error() returns an integer code. preg_last_error_msg() returns the same error as text, which is easier to log and display.
It was added in PHP 8.0. On older versions, compare preg_last_error() with the PREG_* constants yourself.
No. It only reads the last stored error. A later successful preg_* call sets the state back to no error.
Usually no. Log it for developers. Fix the pattern or limits, and show users a generic failure message.

Next: preg_match()

preg_match() tests whether a regular expression matches a string, and optionally fills $ma.

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