note 99657 rejected from function.preg-match by danbrown
| From: | danbrown@php.net | Date: | Sun, 29 Aug 2010 19:27:24 +0000 |
| Subject: | note 99657 rejected from function.preg-match by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172148@lists.php.net to get a copy of this message | ||
Note Submitter: drew at dinomite dot net
----
Though the documentation states that the return value on error will be FALSE (aka bool(false)),
preg_match() in fact returns int(0). Always check preg_last_error(), which gives the truth..
<?php
// Ensure we hit the recursion limit
ini_set('pcre.recursion_limit', 1);
// See http://us.php.net/preg_last_error
$ret = preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
echo 'Return value is ';
var_dump($ret);
if ($ret === false) {
echo '$ret is false; preg_last_error() says: ' . preg_last_error() . "\n";
} else {
echo '$ret is not false; preg_last_error() says: ' . preg_last_error() .
"\n";
}