Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

preg_last_error()

Read preg_last_error immediately after a PCRE operation to get its integer status code.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

preg_last_error(): int

Read preg_last_error immediately after a PCRE operation to get its integer status code. A valid pattern with no match returns zero from preg_match and leaves PREG_NO_ERROR. Invalid UTF-8 under the u modifier can return false with PREG_BAD_UTF8_ERROR. A later match updates the status, so store it before another regex call.

Signature

preg_last_error(): int

Example output

[false,true,0,true]

Examples

<?php
$bad = preg_match('/./u', "\xFF");
$code = preg_last_error();
$miss = preg_match('/z/', "abc");
echo json_encode([$bad, $code === PREG_BAD_UTF8_ERROR, $miss, preg_last_error() === PREG_NO_ERROR]);

Result

[false,true,0,true]

Sources