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...
Auf dieser Seite können Sie das Online-Video PHP 7: Scalar Type Declarations - New Feature! mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer SevenEcks 11 Februar 2018 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 395 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!