S
S
Stadinov Denis2015-10-13 01:03:59
PHP
Stadinov Denis, 2015-10-13 01:03:59

Why does switch have this behavior?

We continue the night column "It's not obvious to me"

switch("a"){
    case 0 : echo "0"; break;
    case "a": echo "a"; break;
}
// получим 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-10-13
@StaDi

Everything is correct. There is a loose comparison.
Run var_dump("a" == 0);
Then, read:
php.net/manual/en/language.types.type-juggling.php
php.net/manual/en/types.comparisons.php
// EDIT
Again, it's all in the documentation. You must read it all initially - at least sections about data types, comparisons, operators, basic functions for working with strings, arrays, numbers.
php.net/manual/en/language.operators.comparison.php
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically
// EDIT2
My last quote is from exactly the same place as your example. You just don't read.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question