PHP preg_last_error() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Regex

Definition

In PHP, preg_last_error() returns an integer code for the last PCRE error from a preg_* function.

After a failed call (bad pattern, backtrack limit, recursion limit, and similar), the code is not PREG_NO_ERROR. Compare it with constants such as PREG_INTERNAL_ERROR or PREG_BACKTRACK_LIMIT_ERROR.

Syntax

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

PHP
$code = preg_last_error();
CallWhat it does
preg_last_error()Returns PREG_NO_ERROR (0) when the last preg_* call succeeded.
preg_last_error() === PREG_NO_ERRORTrue when there was no PCRE error.
preg_last_error() === PREG_INTERNAL_ERRORTrue after some failures, such as a broken pattern.

Call preg_last_error() right after a preg_* function returns false or null unexpectedly. For a readable message, use preg_last_error_msg() instead of memorizing every code.

Library declarations
PHP
preg_last_error(): int

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
intA PCRE error code. PREG_NO_ERROR (0) means the last operation had no error.

PHP Examples

Example 1 — Check After a Successful Match

Run a valid preg_match() and read the error code.

PHP
<?php
preg_match('/\d+/', 'item 42');
echo preg_last_error(), PHP_EOL;
echo preg_last_error() === PREG_NO_ERROR ? 'ok' : 'error', PHP_EOL;

How It Works

  1. The pattern /\d+/ is valid and matches 42.
  2. preg_last_error() returns 0, which is PREG_NO_ERROR.
  3. Comparing with PREG_NO_ERROR confirms success.
  4. Use this check in code that must distinguish success from failure.

Example 2 — Detect a Bad Pattern

An invalid pattern makes the preg_* call fail. Then read the error code.

PHP
<?php
$result = @preg_match('/(/', 'test');
var_dump($result);
echo preg_last_error(), PHP_EOL;
echo preg_last_error() === PREG_NO_ERROR ? 'ok' : 'failed', PHP_EOL;

How It Works

  1. The pattern /(/ is invalid because the opening parenthesis is not closed.
  2. preg_match() returns false (an error), not 0 (no match).
  3. preg_last_error() returns a non-zero code (often PREG_INTERNAL_ERROR).
  4. The @ only hides the warning so the demo stays quiet; real code should fix the pattern.

Example 3 — Reset Expectation After a Good Call

Show that a later successful call clears the error state for practical checks.

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

preg_match('/ok/', 'ok');
echo 'after good: ', preg_last_error(), PHP_EOL;

How It Works

  1. First call uses a broken pattern and sets a non-zero error code.
  2. Second call uses a valid pattern and succeeds.
  3. preg_last_error() is now 0 again.
  4. Always read the error immediately after the call you care about.

Frequently Asked Questions

It returns an integer code describing the last error from a preg_* function. 0 (PREG_NO_ERROR) means no error.
When a preg_* function returns false or null and you need to know why — for example a bad pattern or a backtrack limit.
No. No match returns int 0. false means an error occurred. Use preg_last_error() to inspect that error.
It means the match needed more backtracking steps than PHP allows. Complex or “evil” patterns on long strings can trigger it. Simplify the pattern or raise the limit carefully.
Use preg_last_error_msg(). It returns a short string for the same last error that preg_last_error() reports as a code.

Next: preg_last_error_msg()

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

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