- View Offline
- [Voiceover] PHP's philosophy has always been to make things easy with regard to Data Types. When PHP 3 was released in 1998, the official announcement described PHP as a very programmer-friendly scripting language suitable for people with little or no programming experience, as well as the seasoned web developer who needs to get things done quickly. A key feature of this ease of use is Weak Typing. When you create a variable, there's no need to specify the data type that it's going to store.
In fact, you can't. There's no mechanism to do so. The data type is assigned automatically. If the value's in quotes, it's a string. If it's not in quotes and it's a numeric value, it's an integer or a floating point number. But the data type isn't fixed. PHP won't complain if you assign a string to a variable that previously stored a number. The online manual goes so far as to say that the PHP engine prefers to convert variables to the required data type at runtime.
PHP refers to this as type juggling. PHP has eight different data types. Scalar data types store a single value. Boolean stores either true or false. Integer stores whole numbers. Float stores numbers with a decimal point. And string stores text. Compound data types store multiple values. There are two compound types, array and object. PHP also has two special types.
Resource holds a reference to an external resource, such as a database connection or a file that has been opened by PHP. Null represents no value. Type Juggling, switching data types automatically at runtime, has both advantages and disadvantages. Among the advantages, it hides complexity from the programmer. PHP infers the data type from the context. This is particularly useful for handling user input from online forms.
All values submitted from the form are strings. There's no need to convert them to numbers to perform a calculation. But there are disadvantages. You can get unexpected results. And because PHP doesn't raise an error when type juggling occurs, this can lead to bugs that are difficult to identify. Although there's no mechanism to set a variable as data type, you can use Type Casting to explicitly convert or coerce a value to a specific data type at runtime.
Let's say $myVar has the value 42, but you need to pass $myVar to a function that expects an array. To coerce the value to an array, you precede with a cursing operator, which consists of the data type in parentheses. This casts $myVar to an array containing the single value 42. As PHP has grown in popularity, the debate over strict versus weak data types has intensified. PHP is now used to build complex sophisticated applications.
And many developers have demanded a stricter attitude towards data types, to prevent errors resulting from the wrong type of data being passed to a function or method. But the core developers who maintain and extend PHP have fiercely resisted moving the language away from its user-friendly roots. What has evolved is a compromise. Starting with the release of PHP 5 in 2004, various levels of strict data typing have been gradually introduced. PHP 7 adds new levels and makes important changes to error handling.
But strict typing remains completely optional. There's no obligation to change how you write scripts in PHP 7. But if you do want the extra control of strict data types, it's there for you to use. In the rest of this chapter, we'll take a quick look at type juggling and type casting, before looking at how strict typing is implemented.
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