3v4l.org

run code in 300+ PHP versions simultaneously
<?php function accept_int(int $value) { return $value; } function accept_float(float $value) { return $value; } function accept_string(string $value) { return $value; } function accept_bool(bool $value) { return $value; } $scalarTypes = ['int', 'float', 'string', 'bool']; $data = [ 'int' => 1, 'int-negative' => -1, 'int-zero' => 0, 'float' => 1.2, 'float-negative' => -1.2, 'float-int' => 1.0, 'float-zero' => 0.0, 'string' => 'abc', 'string-empty' => '', 'string-space' => ' ', 'string-int' => '1', 'string-int-negative' => '-1', 'string-int-zero' => '0', 'string-int-spacepad' => ' 1', 'string-int-start' => '1abc', 'string-int-start-spacepad' => ' 1abc', 'string-float' => '1.2', 'string-float-negative' => '-1.2', 'string-float-int' => '1.0', 'string-float-zero' => '0.0', 'string-float-spacepad' => ' 1.2', 'string-float-spacepad-negative' => ' -1.2', 'string-float-start' => '1.2abc', 'string-float-start-negative' => '-1.2abc', 'string-float-start-spacepad' => ' 1.2abc', 'string-float-start-spacepad-negative' => ' -1.2abc', 'bool-true' => true, 'bool-false' => false, ]; function printValue($value) { if (is_string($value)) { return "\"$value\""; } if (is_bool($value)) { return $value ? 'true' : 'false'; } if (is_float($value)) { return number_format($value, 1); } return $value; } $resultsTable = []; foreach ($scalarTypes as $type) { foreach ($data as $key => $value) { $lineKey = $key . ': ' . printValue($value); $acceptFunction = "accept_$type"; try { $deprecatedFlag = false; $noticeFlag = false; set_error_handler(function (int $errno, string $errstr) use (&$deprecatedFlag, &$noticeFlag) { if ($errno === E_NOTICE) { $noticeFlag = true; } else { $deprecatedFlag = true; } echo $errstr . "\n"; }, E_DEPRECATED | E_NOTICE); $result = $acceptFunction($value); set_error_handler(null); if ($result === $value) { $op = 'ok'; } else { $op = 'coerced to ' . printValue($result); if ($deprecatedFlag) { $op .= ' (D)'; } if ($noticeFlag) { $op .= ' (N)'; } } } catch (Throwable $e) { $op = 'error'; } if (!isset($resultsTable[$lineKey])) { $resultsTable[$lineKey] = []; } $resultsTable[$lineKey][$type] = "$op"; } } echo "\n\nResults table:\n\n"; echo ' | ' . str_pad('data type: data', 50, ' ', STR_PAD_LEFT) . ' | '; foreach ($scalarTypes as $type) { echo str_pad($type, 20) . ' | '; } echo "\n"; echo ' | ' . str_pad('', 50, '-') . ' | '; foreach ($scalarTypes as $type) { echo str_pad('', 20, '-') . ' | '; } echo "\n"; foreach ($resultsTable as $key => $results) { echo ' | ' . str_pad($key, 50, ' ', STR_PAD_LEFT) . ' | '; foreach ($results as $type => $result) { echo str_pad($result, 20) . ' | '; } echo "\n"; }
Output for git.master, git.master_jit, rfc.property-hooks
Implicit conversion from float 1.2 to int loses precision Implicit conversion from float -1.2 to int loses precision Implicit conversion from float-string "1.2" to int loses precision Implicit conversion from float-string "-1.2" to int loses precision Implicit conversion from float-string " 1.2" to int loses precision Implicit conversion from float-string " -1.2" to int loses precision Results table: | data type: data | int | float | string | bool | | -------------------------------------------------- | -------------------- | -------------------- | -------------------- | -------------------- | | int: 1 | ok | coerced to 1.0 | coerced to "1" | coerced to true | | int-negative: -1 | ok | coerced to -1.0 | coerced to "-1" | coerced to true | | int-zero: 0 | ok | coerced to 0.0 | coerced to "0" | coerced to false | | float: 1.2 | coerced to 1 (D) | ok | coerced to "1.2" | coerced to true | | float-negative: -1.2 | coerced to -1 (D) | ok | coerced to "-1.2" | coerced to true | | float-int: 1.0 | coerced to 1 | ok | coerced to "1" | coerced to true | | float-zero: 0.0 | coerced to 0 | ok | coerced to "0" | coerced to false | | string: "abc" | error | error | ok | coerced to true | | string-empty: "" | error | error | ok | coerced to false | | string-space: " " | error | error | ok | coerced to true | | string-int: "1" | coerced to 1 | coerced to 1.0 | ok | coerced to true | | string-int-negative: "-1" | coerced to -1 | coerced to -1.0 | ok | coerced to true | | string-int-zero: "0" | coerced to 0 | coerced to 0.0 | ok | coerced to false | | string-int-spacepad: " 1" | coerced to 1 | coerced to 1.0 | ok | coerced to true | | string-int-start: "1abc" | error | error | ok | coerced to true | | string-int-start-spacepad: " 1abc" | error | error | ok | coerced to true | | string-float: "1.2" | coerced to 1 (D) | coerced to 1.2 | ok | coerced to true | | string-float-negative: "-1.2" | coerced to -1 (D) | coerced to -1.2 | ok | coerced to true | | string-float-int: "1.0" | coerced to 1 | coerced to 1.0 | ok | coerced to true | | string-float-zero: "0.0" | coerced to 0 | coerced to 0.0 | ok | coerced to true | | string-float-spacepad: " 1.2" | coerced to 1 (D) | coerced to 1.2 | ok | coerced to true | | string-float-spacepad-negative: " -1.2" | coerced to -1 (D) | coerced to -1.2 | ok | coerced to true | | string-float-start: "1.2abc" | error | error | ok | coerced to true | | string-float-start-negative: "-1.2abc" | error | error | ok | coerced to true | | string-float-start-spacepad: " 1.2abc" | error | error | ok | coerced to true | | string-float-start-spacepad-negative: " -1.2abc" | error | error | ok | coerced to true | | bool-true: true | coerced to 1 | coerced to 1.0 | coerced to "1" | ok | | bool-false: false | coerced to 0 | coerced to 0.0 | coerced to "" | ok |

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
65.73 ms | 425 KiB | 5 Q