3v4l.org

run code in 300+ PHP versions simultaneously
<?php function force_ssl_admin( $force = null ) { static $forced = false; if ( ! is_null( $force ) ) { $old_forced = $forced; $forced = $force; return $old_forced; } return $forced; } function new_force_ssl_admin( $force = null ) { static $forced = false; if ( ! is_null( $force ) ) { $old_forced = $forced; $forced = (bool) $force; return $old_forced; } return $forced; } $datasets = array( 'null' => array( 'force' => null, 'expected' => false, ), '(bool) true' => array( 'force' => true, 'expected' => true, ), '(bool) false' => array( 'force' => false, 'expected' => false, ), '(int) 1' => array( 'force' => 1, 'expected' => true, ), '(int) 0' => array( 'force' => 0, 'expected' => false, ), '(string) "1"' => array( 'force' => '1', 'expected' => true, ), '(string) "0"' => array( 'force' => '0', 'expected' => false, ), '(float) 1.0' => array( 'force' => 1.0, 'expected' => true, ), '(float) 0.0' => array( 'force' => 0.0, 'expected' => false, ), '(string) "true"' => array( 'force' => 'true', 'expected' => true, ), '(string) "false"' => array( 'force' => 'false', 'expected' => true, // (bool) "false" === true ), 'empty string' => array( 'force' => '', 'expected' => false, ), 'string with only spaces' => array( 'force' => ' ', 'expected' => true, ), 'non-empty array' => array( 'force' => array( 'item' ), 'expected' => true, ), 'empty array' => array( 'force' => array(), 'expected' => false, ), 'non-empty object' => array( 'force' => (object) array( 'item' ), 'expected' => true, ), 'empty object' => array( 'force' => (object) array(), 'expected' => true, ), ); foreach( $datasets as $test => $data ) { list( $force, $expected ) = array_values( $data ); force_ssl_admin( $force ); new_force_ssl_admin( $force ); $old = (bool) force_ssl_admin(); $new = new_force_ssl_admin(); echo str_pad( $test . ': ', max( array_map( 'strlen', array_keys( $datasets ) ) ) + 2 ); $bc = $new === $old; $assert = $new === $expected; if ( $bc && $assert ) { echo 'Matches expected value and old value casted to bool.'; } if ( ! $bc ) { echo 'BC break. '; } if ( ! $assert ) { echo "Incorrect value. \n"; echo '-', var_export( $expected, true ), "\n"; echo '+', var_export( $new, true ), "\n"; } echo "\n"; }
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.20, 8.3.0 - 8.3.8
null: Matches expected value and old value casted to bool. (bool) true: Matches expected value and old value casted to bool. (bool) false: Matches expected value and old value casted to bool. (int) 1: Matches expected value and old value casted to bool. (int) 0: Matches expected value and old value casted to bool. (string) "1": Matches expected value and old value casted to bool. (string) "0": Matches expected value and old value casted to bool. (float) 1.0: Matches expected value and old value casted to bool. (float) 0.0: Matches expected value and old value casted to bool. (string) "true": Matches expected value and old value casted to bool. (string) "false": Matches expected value and old value casted to bool. empty string: Matches expected value and old value casted to bool. string with only spaces: Matches expected value and old value casted to bool. non-empty array: Matches expected value and old value casted to bool. empty array: Matches expected value and old value casted to bool. non-empty object: Matches expected value and old value casted to bool. empty object: Matches expected value and old value casted to bool.
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9
null: Matches expected value and old value casted to bool. (bool) true: Matches expected value and old value casted to bool. (bool) false: Matches expected value and old value casted to bool. (int) 1: Matches expected value and old value casted to bool. (int) 0: Matches expected value and old value casted to bool. (string) "1": Matches expected value and old value casted to bool. (string) "0": Matches expected value and old value casted to bool. (float) 1.0: Matches expected value and old value casted to bool. (float) 0.0: Matches expected value and old value casted to bool. (string) "true": Matches expected value and old value casted to bool. (string) "false": Matches expected value and old value casted to bool. empty string: Matches expected value and old value casted to bool. string with only spaces: Matches expected value and old value casted to bool. non-empty array: Matches expected value and old value casted to bool. empty array: Matches expected value and old value casted to bool. non-empty object: Matches expected value and old value casted to bool. empty object: Incorrect value. -true +false

preferences:
344.86 ms | 405 KiB | 473 Q