1. 36
    Type coercion & strict data types in PHP
    4m 26s

Type coercion & strict data types in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet

PHP "juggles" data types with something called "type coercion". Let's learn all about that!

Instructor: [0:00] Now that we have data types for return values and arguments set up, every time data is returned from a function or passed to it, it will be casted or converted into that data type. [0:13] For example, if we call getPostText with the value of 1., which is a type of float rather than int, and save and refresh the page, we will see that everything still works. The value of three posts change to three post, but it still executes.

[0:36] This is because the value of num post is casted to an integer when it's passed into this function.

[0:44] We can confirm this by calling the var_dump function and outputting 1.. We will do the same within the function, var_dump, and then we will output numPosts to see how this value changes.

[1:03] Now when we save and refresh the page, we will see the first var_dump that executes is a float, and the second-time var_dump is called, it's outputted as an int. This conversion process that takes place is something called type coercion.

[1:22] You can also see that the code executes successfully, even though we passed in code that contained a data type that was different from the type our function argument expected it to be.

[1:32] Type coercion seems like some sort of magical helper, but in actuality, it can lead to a bunch of nasty errors down the line. This is because PHP is automagically converting data values to other data types in the background.

[1:49] That is probably not desired when you want to write code that, when given the same input, consistently returns the same output.

[1:56] You can essentially turn off this type coercion by enabling something called strict types. At the top of our file, right after the open PHP tag, let's call the declare construct. This declare keyword lets you set specific PHP directives for a block of code or file that is being executed.

[2:19] We can toggle on the strict types directive by passing in a strict_types value and set it equal to one. Now when we save and refresh the page, we will see that the function doesn't even execute, but a fatal error is thrown. If you want to write reliable code, this is exactly what you want to happen.

[2:44] You'll see that you get a very specific error that says "argument number one, num posts must be of type int float given." This is a great error, because it tells you exactly what is going on. We can also see exactly where this error occurs in the next couple lines.

[3:04] Let's check out line 31 and 21. 31 is the code that we are calling the function with, and 21 is the argument that is being passed into this getPostText function.

[3:19] This first line of line 31 tells us what piece of code is initiating the error. The second line, defined after it, tells us exactly what rule is causing that error to occur.

[3:35] If we check out this line 31, we will also see that PhpStorm is flagging a specific error, right in the editor. This is the same error, "expected parameter of type int float provided." It's very explicit.

[3:51] This is another great reason to use an IDE such as PhpStorm, because it will notify you of errors that occur before you even execute the program. Let's go ahead and remove these var_dump calls, and go ahead and replace our call to this getPostText function back with the num posts variable.

[4:14] Now when we save and refresh the page, we can see that no fatal errors occur because we're passing the value into the function with the desired argument type.