Okay, so next up, I'd like us to learn about the types of the function arguments and the optional
return types that you can define.
Now let's go back to the data types as well.
Let me zoom in.
So we're gonna create this example in a separate file now because this time we're gonna be talking about
the explicit types.
And how can we enforce the type to be explicit.
Which means that you shouldn't really be able to pass strings when the function expects integers.
So let's jump to the code editor and see what does it all mean.
So let's make a types file inside the functions folder, add a PHP tag, and let's define a super simple
function.
But this time we're gonna have types for the arguments and for the return type of that function.
So this function is super simple.
It's just adding.
Now obviously we have an operator for the adding operation in PHP, but that's just an example.
So let's not think about it too much.
And when we add the values we have basically one value that we add to the other.
So let's define two arguments of the type integer a and b.
And obviously the return type of that has to be integer.
So let's also define that.
It's super simple.
We're just going to return a plus b.
So as I said let's ignore the fact that creating such function has zero sense because this operation
exists I just want to present something okay, so why don't we just echo the result of add and let's
add 5 to 3.
Let's run it.
Um, let's make sure I am in the functions folder and run types.
PHP.
So that's the expected result.
Eight.
But what happens if I would pass a string here?
That would be a number but passed as the string type.
So let's run this again.
PHP types PHP and we still got the result back.
Eight.
Now why won't we vardump that instead.
So we will also see the return type.
Let's do var dump and rerun that again.
So the result of the function is an integer value is eight.
So what happened here.
We've defined the types and we were explicit about that.
So what's the point of defining the types if you can pass a string to a function.
So let me explain what's happening here.
So generally the type declarations were introduced in PHP around the version five, and they were just
added gradually.
PHP was moving really slow in the past.
Now a lot more features are added and a lot more quicker and a lot more often.
But that wasn't the case back then.
So you've got the types, but they weren't strictly enforced.
Now, since PHP version seven, another option was added that was strict types, which basically meant
that you had to tell PHP that if you define the types you really mean that that you are not just signaling
what would be nice to pass to a function, but that a value of this specific type has to be passed to
a function.
Otherwise there will be an error, a runtime error, and the program will stop.
So to actually enforce this.
To enforce those types, you need to add a declaration at the top of the file which is looking exactly
like this.
You write declare and you type strict underscore types equals one.
By the way, that's one of the well, one of the couple statements, one of the exceptions in PHP that
does not require semicolon.
So I've mentioned that before that you don't have to add semicolon everywhere.
That's one of the cases where you don't have to do that.
Another is for example namespace declarations.
But we're gonna get to that when we'll be talking about classes.
But I prefer to just add those semicolons everywhere.
My personal preference.
Now let's see what will happen now.
Now we have strict types, but only for this file.
So you need to understand that that you have to add this declaration in every single file.
Now, when I run the same code and I'm passing a string to this function, I get a fatal error.
And I would really suggest to declare the strict types to be enabled, because that's the whole point
of having types, the added security, making sure that no one will accidentally pass something that's
of a different type, which forces PHP to use type coercion, which might have some unexpected side
effects.
So this type coercion, that was fine when PHP was used to create apps that maybe weren't so mission
critical.
But these days when we are creating software that, for example, handles money well, you probably
want to have all the security that you can get So to make it work, I need to change it back to five.
And now we get the working function.
So why we are here?
Let's do an experiment and let's pass a float value.
But we are gonna pass something that is, at least for us, exactly the same.
So I'm going to pass 5.0.
And let's see what PHP is gonna say about that.
Okay.
So that's a fatal error.
So as you see if that's an integer, it has to be an integer, not something that looks like an integer
or is almost an integer.
It has to be an integer.
And it really has to be of this type.
So if you really want to pass this as an integer, you can typecast it using this int operator.
Then it's fine.