What is Scalar Type Hints in PHP 7

QuestionsCategory: PhpWhat is Scalar Type Hints in PHP 7
VR Soft Coder Staff asked 11 months ago

1 Answers
VR Soft Coder Staff answered 11 months ago

In PHP 7 we can add Scalar types like int, float, string, and bool.

By adding scalar type hints and enabling strict requirements, we can write more corrected and self-documenting PHP programs. It also gives us more control over our code and can make the code easier to read.

Strict Example:-

function getSUM(float $a, float $b) {
    return $a + $b;
}
getSUM(5, "2 day");
// you will get a 'Notice: A non well formed numeric value encountered'

getTotal(3.9, '5.1');
// string '5.1' changed to float(3.9) no notice
//returns float(9)

Your Answer

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top