php, mysql, arrays - if( x == 0 ) { }
I have the following code
while($row = $usafisRSP->fetch_assoc()) { $hidden_keys = array('Applicantid', 'unique_num', 'regs_time' ....); $hidden_fields = array_intersect_key($row, array_fill_keys($hidden_keys, NULL)); $hidden_values = array(); foreach ($hidden_fields as $key =开发者_JAVA技巧> $value) { // fill the values array using the values from fields array $hidden_values[$value] = "$key = ".base64_decode($value).""; if(base64_decode($value)== 0) { $hidden_values[$value] = ""; } echo $hidden_values[$value];
The question is about "if($hidden_values[$value] == 0)" ... Basically I want to do not display/echo the $hidden_values[$value] if it's value of $value is 0. Sometimes $value is 0 or some words like (23 avenue).
I think you ran into three catches with PHP type comparisons and equalities:
- Any string not beginning with a number will always loosely equal 0. So basically,
if(base64_decode($value)== 0)
will likely always resolve to true, even if decoded$value
is"Adam"
. - Return value of base64_decode is a string, so if 0 is the result, it will be string 0, not integer 0. This means
if(base64_decode($value) === 0)
wouldn't even work if decoded$value
is"0"
. Another catch is base64_decode may return false on errors, again failing this strict equality check. - A non-empty string (other than
"0"
) will always loosely equal true. So this is the only comparison you really need for your case.
I think this is what you want, replacing the last 5 lines...
if(base64_decode($value)) echo $hidden_values[$value];
else $hidden_values[$value] = "";
} // closing your for loop
Is this what you're looking for?
foreach( $hidden_values as $value ) {
if( $value !== 0 ) {
echo $value;
}
}
精彩评论