Monday, 15 September 2014

Why does PHP consider 0 to be equal to a string? -



Why does PHP consider 0 to be equal to a string? -

i have next piece of code:

$item['price'] = 0; /*code item info goes in here*/ if($item['price']=='e'){ $item['price'] = -1; }

it intended initialize item cost 0 , info it. if cost informed 'e' means exchange instead of sell, indicated negative value because stored in database requires numeric value.

there possibility leave cost 0, either because item bonus or because cost set in later moment.

but, when cost not set, leaves initial value of 0, if loop indicated above evaluates true , cost set -1. is, considers 0 equal 'e'.

how can explained?

edit: when cost provided 0 (after initialization), behaviour erratic: if evaluates true, evaluates false.

you doing == sorts out types you.

0 int, in case going cast 'e' int. not parseable one, , become 0. string '0e' become 0, , match!

use ===

php string numbers evaluate

No comments:

Post a Comment