Telling the truth – The Daily WTF

Many languages ​​eschew “truth” for “true”. Today we look at PHP’s approach.

PHP automatically coerces types to boolean with some fairly simple rules:

  • Boolean false is fake
  • whole number 0 is false, as is float 0.0 and -0.0.
  • empty strings and string "0" are false
  • arrays with no elements are false
  • NULL is fake
  • objects can also override cast behavior to define their own
  • everything else is true

Honestly, it’s pretty sane and reasonable for PHP. series "0" makes my skin crawl a little, and the fact that the documentation page needs a big disclaimer -1 is the truth hints that some people make mistakes when switching from other languages.

But we are not dealing with language criticism. We are 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 )  (float) 1 === $value ) ) 
		return (bool) $value;
	 elseif ( is_string( $value ) ) 
		$value = trim( $value );
		if ( in_array( $value, $true, true ) ) 
			return true;
		 elseif ( in_array( $value, $false, true ) ) 
			return false;
		 else 
			return false;
		
	

	return false;

I like when a block of code starts with a comment to force the linter to ignore them. Always great.

We start by declaring arrays of different possible true and false values. Most variations are capitalized, but no exhaustive variations in capitalization, which I’m sure will cause problems at some point. But let’s see how this code works.

First we check if the value is a Boolean, and if it is, we return it. Okay, very reasonable.

Second, we check to see if it’s an integer, and if it’s 0 or 1, we convert it to a Boolean value and return it. In pure PHP, anything non-zero is true, but only here 1 is true.

Then we do a similar check for floats – if it’s a float, then it is is number, which is either 0 or 1, we convert it to a float and return it. Note, however, that they do a simple equality comparison – meaning that it enters with a floating point like 1.000000001 will not fail this test, but will often still print as 1 when formatted for printing, which makes developers very confused (ask Jakub how he knows).

Finally, we check the strings and do an in_array comparison to check if the value is true or false. If it is neither true nor false, we simply return false (instead of FILE_NOT_FOUND). This raises the obvious question: why bother checking for an array of false values ​​at all, when we can just assume an entry is false if it’s not in an array of real values?

Jakub has this to add:

This method finds application in a number of places, especially where front-end interfaces display select boxes. I’m really afraid to reveal the full extent of the code responsible for this…

[Advertisement]

Continuously monitor your servers for configuration changes and report if configuration drift occurs. Get started with Otter today!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *