Adsense
Popular Posts
- MySQL workbench -"Could not decrypt password cache"
- Install APXS in Redhat Linux
- react-pdf, display pdf in react.js
- Set Windows path command line
- MySQL date created and date modified
- Transfer modules between sites
- DataTable table order by numeric, not by text
- datatable order by nunmeric
- mod_auth_cas.so error: undefined symbol: SSL_connect
- JavaScript, remove trailing insignificant zeros after toFixed function
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;
Subscribe to:
Comments (Atom)