0

I found this, when I'm working in regex validation in PHP.

I've used preg_match function to validate a string.

I've just printed that value returned by preg_match to ensure working flow.

During that I've found a thing. But can't understand their concept.

That was preg_match() returns 1 if the match found. Otherwise, it will return 0. But sometimes my print function didn't print anything.

After I went through the PHP manual to know all of its return value...

There they posted as follows...

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

And then I used var_dump to know anything is printed or not...

This helped me to know that's a Boolean false.

But I'm curious to know why it returns boolean false, when I'm just putting " ! " (not) before the preg_match()?

Following is my code,

echo preg_match("/script/", "script") // ===> this returns 1

echo !preg_match("/script/", "script") // ===> this returns Boolean false

I think it has to return integer 0 (zero)... What is its functionality? Or did I do anything wrong in syntax?

I've tried this in the OpenCart 2.0.0.0 system administrator controller module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GoCool
  • 7
  • 1
  • 8
  • `!` is a logical operator. It returns either `1` (true) or `0` (false). Since anything non-zero is regarded as true, the value of `!1` is equal to false. (By the way, this question has nothing to do with regex, preg-match or opencart. I suggest you remove those tags.) – r3mainer Aug 19 '15 at 16:33
  • 2
    `!*expression*` returns `(*expression* == false)`. Here `*expression*` returns 1, as `(1 == false)` is false, `!*expression*` returns false. http://stackoverflow.com/questions/13029921/exclamation-mark-in-front-of-variable-clarification-needed –  Aug 19 '15 at 16:33
  • @caCtus Good find. I'm going to flag this question for closing. – r3mainer Aug 19 '15 at 16:34
  • sorry for the burden i caused – GoCool Aug 19 '15 at 16:52

2 Answers2

2

The ! operator will always return a Boolean value.

For a unary ! operator the type of the result is bool. The value of the operand is converted to type bool and if it is TRUE then the of the operator result is FALSE, and the result is TRUE otherwise.

Language specification reference: Expressions

The normal conversion rules to a bool are applied to the value.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rangad
  • 2,110
  • 23
  • 29
  • The Markdown in the reference seems to be invalid (not valid [CommonMark](https://en.wikipedia.org/wiki/Markdown#CommonMark) or similar). – Peter Mortensen Feb 23 '22 at 21:42
0

You're not echoing preg_match's return value when you put a ! in front of the function. You're using PHP's operator to determine if it loosely evaluates to true or false.

!preg_match("/script/","script") is the same as preg_match("/script/","script") == false. Note the == and not the ===. 0, null, empty string, and Boolean false will evaluate loosely to false.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95