PHP 7: Scalar Type Declarations - New Feature!

Published: 11 February 2018
on channel: SevenEcks
395
3

Scalar Type Declarations

Watch my video on Scalar Variables:    • What is a Scalar Variable?  

My video on return type declarations:    • PHP 7: Return Type Declarations - New Feat...  

1. Introduced in php 5.6 as type hints and having undergone some changes, in PHP 7 they are now declarations
Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.
2. Types of Scalar: INT, FLOAT, STRING, BOOL
Aliases for the above scalar types are not supported. So use bool, not boolean, or you'll have an error or it'll be treated as an instance of the Boolean class.
If you want more information on Scalar variables, check out my video What are Scalar Variables?
3. PHP has weak typing. By default PHP will coerce scalar values from one type to another
if it can't, IE: bool but expecting array, a TypeError will be thrown
4. Strict typing can be turned on, on a file by file basis
the exception to this is that an integer can be passed to a declaration expecting a float
To enable strict mode, the declare statement is used with the strict_types declaration:
turning on strict mode enables it for return type declartions as well
Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. If a file without strict typing enabled makes a call to a function that was defined in a file with strict typing, the caller's preference (weak typing) will be respected, and the value will be coerced.

More Info on Scalar Type Declarations: https://secure.php.net/manual/en/migr...


On this page of the site you can watch the video online PHP 7: Scalar Type Declarations - New Feature! with a duration of hours minute second in good quality, which was uploaded by the user SevenEcks 11 February 2018, share the link with friends and acquaintances, this video has already been watched 395 times on youtube and it was liked by 3 viewers. Enjoy your viewing!