CodeSOD To Tell the Truth
CodeSOD To Tell the Truth (Porter, 2024)
Metadata
- Author: Remy Porter
- Full Title: CodeSOD To Tell the Truth
- Category: #articles
- Summary: So many languages eschew "truth" for "truthiness". Today, we're looking at PHP's approach. PHP automatically coerces types to a boolean with some fairly simple rules:
the boolean false is false the integer 0 is false, as is the float 0.0 and -0.0. empty strings and the string "0" are false arrays with no elements are false NULL is false objects may also override the cast behavior to define their own everything else is true
Honestly, for PHP, this is fairly sane and reasonable. The string "0" makes my skin itch a bit, and the fact that the documentation page needs a big disclaimer warning that -1 is true hints at some people blundering as they convert from other languages. But we're not doing a language critique. We're looking at a very specific block of code, which Jakub inherited. You see, someone didn't like this, so they implemented their own version: protected static function emulate_filter_bool( $value ) { // @codingStandardsIgnoreStart static $true = array( '1', 'true', 'True', 'TRUE', 'y', 'Y',...
Highlights
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)
- { highlight_text }}_ (View Highlight)