Join David Powers for an in-depth discussion in this video Overview of scalar type declarations, part of PHP: Strict Data Types.
- [Voiceover] In this chapter, we'll take an in-depth look…at scalar type hints in PHP 7.…There are four scalar types.…Int for integers, float for floating point numbers,…string for strings, and bool for Boolean true or false.…These are the only four keywords that you can use.…You can't use integer or Boolean…as aliases for int and bool.…From PHP 7.1, you can also use void…as a return type declaration for a function…that doesn't return anything,…not even an empty string or null.…
You can use scalar type declarations…for both parameters and return types.…The syntax is exactly the same…as for objects, arrays, and callable functions.…Parameter type declarations preceded the paramater…in the function signature,…and return type declarations come after…the function signature, separated by a colon.…A major difference with scalar type declarations…is that data types are enforced in either weak…or strict mode.…Understanding how the modes work…is vital to working with scalar type declarations.…
Weak mode is the default for scalar type declarations.…
First, he introduces the basics of type juggling and type casting in PHP. Then, he reviews strict typing with objects, arrays, and callback functions, which were introduced in PHP 5. He also reviews scalar type hints in PHP 7, which can have unexpected side effects for unwary developers. David concludes with a frank assessment of the practicalities of strict typing, and a simple recommendation that will help most developers use the new feature: define in strict and execute in weak.
- Implicit and explicit type casting
- Using strict data types
- Declaring return types in PHP 7
- Creating scalar parameter declarations and scalar return type declarations
- Assessing the merits of strict data types