vendredi 30 janvier 2015

Strange php if behaviour with casted to boolean int

let's see if somebody can help me.


I have function called ret_error() which is called when there's an error accessing a table in a database, that means, "could not connect", "record not found" and this like that. This functions returns an array with two keys like this



array('error' => true, 'code' => Model::RECORD_NOT_FOUND);


If I call a function that returns an array built with ret_error() and I inspect the array it is like



$result = array('error' => 1, 'code' => Model::RECORD_NOT_FOUND);


But when I evaluate with an 'if'



if (!$result['error']) {
do something;
}
else {
do something else;
}


Always is evaluated as false even if I cast to a boolean.


This works correctly if evaluated as int such as



if ($result['error'] != 1) {


Is there something I'm missing?.


Aucun commentaire:

Enregistrer un commentaire