Adsense
Popular Posts
- MySQL workbench -"Could not decrypt password cache"
- mod_auth_cas.so error: undefined symbol: SSL_connect
- JavaScript, remove trailing insignificant zeros after toFixed function
- Set Windows path command line
- JavaScript Arrays and Associate Arrays
- Design date and signature box in Latex
- Install APXS in Redhat Linux
- super(props) in React
- jQuery, toggle the display
- PHP function for input sanitizing
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:
Posts (Atom)