<?php
function add(float $a, float $b): float {
return $a + $b;
}
var_dump(add("1", "1"));
// float(2)
var_dump(add("1", "1foo"));
// PHP Notice: A non well formed numeric value encountered
// float(2)
var_dump((float) "foo");
// float(0)
var_dump(floatval("foo"));
// float(0)
var_dump(add("1", "foo"));
// PHP Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type float, string given
float(2)
Fatal error: Uncaught TypeError: add(): Argument #2 ($b) must be of type float, string given, called in /in/4OQeZ on line 9 and defined in /in/4OQeZ:3
Stack trace:
#0 /in/4OQeZ(9): add(1.0, '1foo')
#1 {main}
thrown in /in/4OQeZ on line 3
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
float(2)
Fatal error: Uncaught TypeError: add(): Argument #2 ($b) must be of type float, string given, called in /in/4OQeZ on line 9 and defined in /in/4OQeZ:3
Stack trace:
#0 /in/4OQeZ(9): add(1.0, '1foo')
#1 {main}
thrown in /in/4OQeZ on line 3
Process exited with code 255.
Output for 8.0.0 - 8.0.30
float(2)
Fatal error: Uncaught TypeError: add(): Argument #2 ($b) must be of type float, string given, called in /in/4OQeZ on line 9 and defined in /in/4OQeZ:3
Stack trace:
#0 /in/4OQeZ(9): add(1, '1foo')
#1 {main}
thrown in /in/4OQeZ on line 3
Process exited with code 255.
float(2)
Notice: A non well formed numeric value encountered in /in/4OQeZ on line 3
float(2)
float(0)
float(0)
Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type float, string given, called in /in/4OQeZ on line 16 and defined in /in/4OQeZ:3
Stack trace:
#0 /in/4OQeZ(16): add(1, 'foo')
#1 {main}
thrown in /in/4OQeZ on line 3
Process exited with code 255.
Output for 7.3.32 - 7.3.33
float(2)
float(2)
float(0)
float(0)
Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type float, string given, called in /in/4OQeZ on line 16 and defined in /in/4OQeZ:3
Stack trace:
#0 /in/4OQeZ(16): add(1, 'foo')
#1 {main}
thrown in /in/4OQeZ on line 3
Process exited with code 255.