Type coercion & strict data types in PHP

Now that we have data types for return values & arguments set up, every time data is return from a function or passed to it, it will be casted, or converted, into that data type.

For example, if we call getPostText wtih the value of 1.0 which is of type float rather than int, and reload the page, we will see that everything still works!

This is because the value of $numPosts casts the value to an int. We can confirm this by calling the var_dump function to dump out the value before the function and pass in 1.0, and then within the function calling var_dump to dump out the value in the function of $numPosts. We can see that PHP is converting the value from a float to an int. This conversion is called “type coercion”. 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.

Type coercion seems like a magical helper, but inactuality 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, and that is probably not desired when you want to write code that when given the same intput, consistently returns the same output.

You can essentially turn off this type coercion by enabling something called “strict types”. At the top of our file right after our 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 exected.

We can toggle on the strict types directive by passing in strict_type, and setting it equal to 1.

<?php declare(strict_types=1);
function getPosts(): array

Now when we refresh the page, we can 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! You’ll see that you get a very specific error that says “Argument #1 ($numPosts) must be of type int, float given”. This is a great error because it tells you exactly what is going on.

We also see that this error occurs on line 31 of index.php, but we also see that it includes a Stack trace which tells us which code is calling this function, so we can tell the line of code that is calling this function is happening on line 21.

If we go to line 31 in our IDE, we can also see that PHPStorm is telling us that the expected parameter is of type int, float given, which is the same fatal error that is outputted when we execute our program. 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 change the value passed to getPostText() back to $numPosts. We can also remove the var_dump lines because they are no longer relevant. Now when we re-execute our program by refreshing the page, we can see that no fatal errors occur because we are passing the variable into the function with the desired argument type.

Complete and Continue  
Extra lesson content locked
Enroll to access source code, comments & track progress.
Enroll for Free to Unlock