V
V
Vidog2012-01-25 05:24:11
PHP
Vidog, 2012-01-25 05:24:11

PHP behaves strangely when comparing null and "*"

I'm shocked by php ... Maybe I don't understand something, I didn't find anything in Google. Please run this code:

$a = '0';
$b = 0;
$c = '1';
$d = 1;
if($a == '*') echo 'A = *;<br />';
if($b == '*') echo 'B = *;<br />';
if($c == '*') echo 'C = *;<br />';
if($d == '*') echo 'D = *;<br />';

When compared via == null (precisely int type) with the string '*', it returns TRUE.
Comparison through === and null string ('0') gives FALSE

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
edogs, 2012-01-25
@Vidog

So coercion of types. The right argument is cast to the type of the left argument. Those. '*' is cast to 0, which becomes 0 accordingly.
And this is not only with *, but with anything other than digital. Try a, b, +, etc.

I
Ilya Plotnikov, 2012-01-25
@ilyaplot

you can try
$a = *;
if ($a == strval(0))
In general, I am for variable typing. In 5.4, they have already begun to do this. I'm looking forward to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question