1--TEST-- 2Strict scalar type basics 3--FILE-- 4<?php 5 6declare(strict_types=1); 7 8$functions = [ 9 'int' => function (int $i) { return $i; }, 10 'float' => function (float $f) { return $f; }, 11 'string' => function (string $s) { return $s; }, 12 'bool' => function (bool $b) { return $b; } 13]; 14 15$values = [ 16 1, 17 1.0, 18 "1", 19 TRUE, 20 FALSE, 21 NULL, 22 [], 23 new StdClass, 24 fopen("data:text/plain,foobar", "r") 25]; 26 27function type($value) { 28 if (is_float($value)) { 29 return "float"; 30 } else if (is_bool($value)) { 31 return $value ? "true" : "false"; 32 } else if (is_null($value)) { 33 return "null"; 34 } else { 35 return gettype($value); 36 } 37} 38 39foreach ($functions as $type => $function) { 40 echo PHP_EOL, "Testing '$type' type:", PHP_EOL; 41 foreach ($values as $value) { 42 $errored = false; 43 echo PHP_EOL . "*** Trying ", type($value), " value", PHP_EOL; 44 try { 45 var_dump($function($value)); 46 } catch (TypeError $e) { 47 echo "*** Caught " . $e->getMessage() . PHP_EOL; 48 } 49 } 50} 51echo PHP_EOL . "Done"; 52?> 53--EXPECTF-- 54Testing 'int' type: 55 56*** Trying integer value 57int(1) 58 59*** Trying float value 60*** Caught Argument 1 passed to {closure}() must be of the type int, float given, called in %s on line %d 61 62*** Trying string value 63*** Caught Argument 1 passed to {closure}() must be of the type int, string given, called in %s on line %d 64 65*** Trying true value 66*** Caught Argument 1 passed to {closure}() must be of the type int, bool given, called in %s on line %d 67 68*** Trying false value 69*** Caught Argument 1 passed to {closure}() must be of the type int, bool given, called in %s on line %d 70 71*** Trying null value 72*** Caught Argument 1 passed to {closure}() must be of the type int, null given, called in %s on line %d 73 74*** Trying array value 75*** Caught Argument 1 passed to {closure}() must be of the type int, array given, called in %s on line %d 76 77*** Trying object value 78*** Caught Argument 1 passed to {closure}() must be of the type int, object given, called in %s on line %d 79 80*** Trying resource value 81*** Caught Argument 1 passed to {closure}() must be of the type int, resource given, called in %s on line %d 82 83Testing 'float' type: 84 85*** Trying integer value 86float(1) 87 88*** Trying float value 89float(1) 90 91*** Trying string value 92*** Caught Argument 1 passed to {closure}() must be of the type float, string given, called in %s on line %d 93 94*** Trying true value 95*** Caught Argument 1 passed to {closure}() must be of the type float, bool given, called in %s on line %d 96 97*** Trying false value 98*** Caught Argument 1 passed to {closure}() must be of the type float, bool given, called in %s on line %d 99 100*** Trying null value 101*** Caught Argument 1 passed to {closure}() must be of the type float, null given, called in %s on line %d 102 103*** Trying array value 104*** Caught Argument 1 passed to {closure}() must be of the type float, array given, called in %s on line %d 105 106*** Trying object value 107*** Caught Argument 1 passed to {closure}() must be of the type float, object given, called in %s on line %d 108 109*** Trying resource value 110*** Caught Argument 1 passed to {closure}() must be of the type float, resource given, called in %s on line %d 111 112Testing 'string' type: 113 114*** Trying integer value 115*** Caught Argument 1 passed to {closure}() must be of the type string, int given, called in %s on line %d 116 117*** Trying float value 118*** Caught Argument 1 passed to {closure}() must be of the type string, float given, called in %s on line %d 119 120*** Trying string value 121string(1) "1" 122 123*** Trying true value 124*** Caught Argument 1 passed to {closure}() must be of the type string, bool given, called in %s on line %d 125 126*** Trying false value 127*** Caught Argument 1 passed to {closure}() must be of the type string, bool given, called in %s on line %d 128 129*** Trying null value 130*** Caught Argument 1 passed to {closure}() must be of the type string, null given, called in %s on line %d 131 132*** Trying array value 133*** Caught Argument 1 passed to {closure}() must be of the type string, array given, called in %s on line %d 134 135*** Trying object value 136*** Caught Argument 1 passed to {closure}() must be of the type string, object given, called in %s on line %d 137 138*** Trying resource value 139*** Caught Argument 1 passed to {closure}() must be of the type string, resource given, called in %s on line %d 140 141Testing 'bool' type: 142 143*** Trying integer value 144*** Caught Argument 1 passed to {closure}() must be of the type bool, int given, called in %s on line %d 145 146*** Trying float value 147*** Caught Argument 1 passed to {closure}() must be of the type bool, float given, called in %s on line %d 148 149*** Trying string value 150*** Caught Argument 1 passed to {closure}() must be of the type bool, string given, called in %s on line %d 151 152*** Trying true value 153bool(true) 154 155*** Trying false value 156bool(false) 157 158*** Trying null value 159*** Caught Argument 1 passed to {closure}() must be of the type bool, null given, called in %s on line %d 160 161*** Trying array value 162*** Caught Argument 1 passed to {closure}() must be of the type bool, array given, called in %s on line %d 163 164*** Trying object value 165*** Caught Argument 1 passed to {closure}() must be of the type bool, object given, called in %s on line %d 166 167*** Trying resource value 168*** Caught Argument 1 passed to {closure}() must be of the type bool, resource given, called in %s on line %d 169 170Done 171