1--TEST-- 2Scalar type - default via constants 3--FILE-- 4<?php 5 6const INT_VAL = 10; 7const FLOAT_VAL = 10.5; 8const STRING_VAL = "this is a test"; 9const INT_ADD_VAL = 10 + 15; 10const FLOAT_ADD_VAL = 10.5 + 0.2; 11const STRING_ADD_VAL = "this" . " is a test"; 12const NULL_VAL = null; 13 14function int_val(int $a = INT_VAL): int { 15 return $a; 16} 17 18function float_val(float $a = FLOAT_VAL): float { 19 return $a; 20} 21 22function string_val(string $a = STRING_VAL): string { 23 return $a; 24} 25 26function int_add_val(int $a = INT_ADD_VAL): int { 27 return $a; 28} 29 30function float_add_val(float $a = FLOAT_ADD_VAL): float { 31 return $a; 32} 33 34function string_add_val(string $a = STRING_ADD_VAL): string { 35 return $a; 36} 37 38function int_val_default_null(int $a = NULL_VAL) { 39 return $a; 40} 41 42function nullable_int_val_default_null(?int $a = NULL_VAL) { 43 return $a; 44} 45 46echo "Testing int val" . PHP_EOL; 47var_dump(int_val()); 48 49echo "Testing float val" . PHP_EOL; 50var_dump(float_val()); 51 52echo "Testing string val" . PHP_EOL; 53var_dump(string_val()); 54 55echo "Testing int add val" . PHP_EOL; 56var_dump(int_add_val()); 57 58echo "Testing float add val" . PHP_EOL; 59var_dump(float_add_val()); 60 61echo "Testing string add val" . PHP_EOL; 62var_dump(string_add_val()); 63 64echo "Testing int with default null constant" . PHP_EOL; 65try { 66 var_dump(int_val_default_null()); 67} catch (TypeError $e) { 68 echo $e->getMessage(), "\n"; 69} 70 71echo "Testing int with null null constant" . PHP_EOL; 72try { 73 var_dump(int_val_default_null(null)); 74} catch (TypeError $e) { 75 echo $e->getMessage(), "\n"; 76} 77 78echo "Testing nullable int with default null constant" . PHP_EOL; 79var_dump(nullable_int_val_default_null()); 80 81echo "Testing nullable int with null null constant" . PHP_EOL; 82var_dump(nullable_int_val_default_null(null)); 83 84?> 85--EXPECTF-- 86Testing int val 87int(10) 88Testing float val 89float(10.5) 90Testing string val 91string(14) "this is a test" 92Testing int add val 93int(25) 94Testing float add val 95float(10.7) 96Testing string add val 97string(14) "this is a test" 98Testing int with default null constant 99int_val_default_null(): Argument #1 ($a) must be of type int, null given, called in %s on line %d 100Testing int with null null constant 101int_val_default_null(): Argument #1 ($a) must be of type int, null given, called in %s on line %d 102Testing nullable int with default null constant 103NULL 104Testing nullable int with null null constant 105NULL 106