Why in PHP and Mysql ! is it converted to 0 in some cases?

Asked

Viewed 60 times

3

The question is the title by itself, in PHP we can do:

var_dump((int) "!");

And receive int(0).

And in Mysql we can do:

SELECT campo_int FROM tabela WHERE campo_int = "!"

What if this 'campo_int' for '0' he returns the record.

  • Why does this happen?
  • This has some usefulness planned by the language developers?
  • It seems to me that they are two distinct and even opposite behaviors. Swear that Mysql does this?

  • 4

    ! is not a valid int so returns false with the cast applied turns zero. Maybe it’s the same case of that question. Already in Mysql I suspect that you are not configuring as strict mode.

  • @Maniero yes!! I was a little wrong in a query, and noticed that, only returns in fields int other types not

  • 1

    The question is misspelled, Mysql returns anything that does not !. And in fact they are two different things.

1 answer

3


When you execute this command:

var_dump((int) "!");

In fact it returns false since it could not do cast to int, if you do this way for example:

var_dump((int) "qualquerCoisaString");

It will return 0 the same way, since it cannot convert to int, and in mysql it is the same thing, if you do so:

SELECT campo_int FROM tabela WHERE campo_int = "qualquerCoisaString"

It will return the values of the campo_int that has the value 0, since its expression returned 0(false).

  • Really that’s right, it seems that in int if it is not int it assumes the boolean behavior, I found confusing, it seems even a flaw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.