php - unexpected cast to boolean? -
given input: http://example.com/item.php?room=248&supply_id=18823, following 2 blocks ought produce same result. why don't they? missing other coffee?
this block gives expected values:
if (isset($_get['supply_id']) && isset($_get['room'])) { $id=validkey($_get['supply_id']); //18823 $room=validkey($_get['room']); //248 $arr=array('s'=>$id,'r'=>$room); //s=>18823, r=>248 }
but if check , assignment in 1 step, $id ends equal 1 instead of 18823. why?
if (isset($_get['supply_id']) && isset($_get['room'])) { if($id=validkey($_get['supply_id']) && $room=validkey($_get['room'])) $arr=array('s'=>$id",'r'=>$room); //s=>1, r=>248 }
this function i'm using:
function validkey($value){ if(is_scalar($value)){ $value=(int)$value; return ($value>0) ? $value : false; } return false; }
you should use parentheses:
if(($id=validkey($_get['supply_id'])) && ($room=validkey($_get['room'])))
otherwise result of validkey($_get['supply_id']) && $room=validkey($_get['room'])
assigned $id
variable because &&
operator has higher precedence =
Comments
Post a Comment