Tuesday, November 15, 2016

PHP null compare to 0




When PHP gets null from MySQL NULL column and compare to 0
     $ChairRecomStep_arr = array(0,0.5,1,1.5,2);
        $AdditionStepOrder =  $AdditionStepOrder.'</select>';

            $ChairRecomStep =   '<select name="ChairRecomStep" class="ChairRecomStep"><option value="">Select</option>';
         foreach($ChairRecomStep_arr  as $row0){
                    $select=' ';
                    if($row['ChairRecomStep']== $row0) $select='selected';
            $ChairRecomStep =  $ChairRecomStep.'<option value="'.$row0.'" '.$select.'>'.
            $row0.'</option> ';

         }
        $ChairRecomStep =  $ChairRecomStep.'</select>';

In this code, null compare to 0 is true, the select dropdwon pickup 0 value instead of empty.
To make null compare to 0 is false, we should use = = =
   if($row['ChairRecomStep']=== $row0) 

Also when we use empty function in PHP, the 0 also treated as  empty. To avoid 0 treated as empty:
      if(empty($ChairRecomStep) && $ChairRecomStep!=0) $ChairRecomStep=null;