Re: Stricter implicit boolean coercions

From: Date: Wed, 27 Apr 2022 11:30:31 +0000
Subject: Re: Stricter implicit boolean coercions
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-117634@lists.php.net to get a copy of this message
> > 'on' is only true by "accident" though, because it is a non-empty > > string, not because of its meaning, and then it is likely that the value > > 'off' could also be added at some point - which also would be true. > > The reason I gave that particular example is that it's the default > submission value for an HTML checkbox when checked; if it's not checked, > it has no value at all (not even an empty string), so in that particular > context there is no corresponding "off". > That's why you must test it with isset($_POST['checkbox_name']), *not* with (bool)$_POST['checkbox_name'] or an implicit conversion. if ( $nullable_float = type_coerce_or_return_default('?float', $var, > false) === false ) { throw new TypeError; } > Missing parentheses around the assignment (VS the comparison)! Only allowing the following values would make sense > from my perspective: > > '1' => true > 1 => true > 1.0 => true > '' => false > '0' => false > 0 => false > 0.0 => false > Seems a reasonable compromise (between BC and bugs-protection). Regards, -- Guilliam Xavier

« previous php.internals (#117634) next »