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

3

The question is the title alone, 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 = "!"

And if that 'campo_int' is '0' it returns the record.

  • Why does this happen?
  • Does this have any usefulness planned by language developers?
asked by anonymous 09.11.2017 / 16:55

1 answer

3

When you run this command:

var_dump((int) "!");

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

var_dump((int) "qualquerCoisaString");

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

SELECT campo_int FROM tabela WHERE campo_int = "qualquerCoisaString"

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

    
09.11.2017 / 17:14