Welcome to Findnerd. Today we are going to dicuss the strict typing mode and weak typing mode which is introduced in PHP7. strict typing mode is not activated by default.
You need to activate it via declare like this
declare(strict_type=1);
It should be first statement in your file. It will activate the strict typing mode in current file. It will only applied called functions in the file not defined functions.
<?php
declare(strict_types=1);
function userdata(string $name,int $phone){
echo 'name:' . $name . '<br />' . $phone . '<br />';
}
userdata('deepak',8449923503);
userdata('deepak','8449923503');
?>
In above example we have set the strict typing mode. We created a function named userdata with two arguments that are name , datatype is string and another is integer phone
we called the function twice. In second call we passed the phone number as string. It should be integer. It will return the error for correct type.
If you do not declare the strict type to 1, it will be work as strict typing mode. In weak typing it will convert the phone number into a interger. You can also
set the weak typing by this
declare(strict_types=0);
0 Comment(s)