DEV Community

Falak
Falak

Posted on

PHP Error

$valOne variable has string type value and $valTwo variable has original value. I calculated them and did not find error as I excepted.

$valOne = "100"; //defined variable as string.
$valTwo = 6;    //defined variable as value.
$calOfVals = $valOne / $valTwo; //applied division.
echo $calOfVals; //output: 16.666666666667.
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
xwero profile image
david duymelinck •

As Sophie mentioned php is not strictly typed. The most common way to add strict typing is to add declare(strict_types=1); at the top of php files.

But this will not fix your code, because you didn't give php type information.
If you create a function with type hinting then it will give an error.

// an arrow function, or a lambda function in other languages
$divide = fn(int $a, int $b) => $a / $b;

// a regular function
function divide(int $a, int $b) {
    return $a / $b;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
samuraiseoul profile image
Sophie The Lionhart •

PHP isn't as strictly typed as other languages. You will get a warning if you at least turn on strict types and can promote that to an error I believe in your php configs. Its been a while since I did PHP but I know in classes now you can type the member variable but I'm not sure about regular variables.