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


i have following piece of code:

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

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

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

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

how can explained?

edit: when price provided 0 (after initialization), behavior 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 ===


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -