Model Attribute Return 0 For Null Value

I need help to differentiate between 0 and NULL values on Yii 1.1.8 models. Apparently, when an integer field in mysql is set to NULL, Yii returns 0 instead. I wonder if Yii converts the NULL value to 0 automatically? This should not be the case as 0 is not necessarily NULL. Please help.

Hi monk,

PHP uses implicit type conversion frequently.

$value = NULL;

if ($value == 0) {

   echo 'Yes, it is zero. Ah, well, kind of ...';


Isn’t it because you are comparing the value with the operator ‘==’ ?

This has worked now. So sorry I was quick to raise it here. It was my fault I hadn’t checked the data that was coming from mysql quite well.

Yes, I was using ‘==’ instead of checking for equality using ‘===’.

This works now. thanks.

Don’t be sorry, no problem.

function is_empty($input){
return strlen((string)trim($input)) != 0 && empty($input);

if $input have any thing return True else if have NULL or null or ‘’ return false