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

Posted on

Question :

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?

Answer :

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).

    

Leave a Reply

Your email address will not be published. Required fields are marked *