Okay.
So let's learn about the union type.
So we're going to learn with functions.
So let's create a union PHP file.
And let's explain what it is all about.
So as usual we're going to create a function.
So let me call this process input.
And the thing is that the input the argument it can take different types.
So first let's declare the strict types here.
Like this.
And let me add the function body.
So the input I'd like it to be either integer but also float and string.
How can we achieve that if I've already typed that as int?
Well this is where union types are for.
So I use this.
Let's call it pipe Type character or alternative character.
And this way I just define alternative types.
So if I want int, float and string, this is how I do that now.
All those types can be passed to this function.
But if I need to then work with this data that is being passed, it's my job to just check what's the
type.
Obviously if I need this information.
Next up, depending on the type of the passed argument, we'd like to pass it through some different
processing rules.
And maybe let's again use the match expression after we initially explained it.
I don't think we have ever used it again.
So let me show you something.
So we can use match expression by just matching the true value.
So then in every individual case, I can actually call any function Like I can call the is int function
to check if the input is an integer, and then I can output something something like integer and concatenate
the input text.
Maybe I will just multiply this integer.
Next up I'm gonna check if it is a float.
Then we're gonna return the text float and concatenate that with rounding the input.
And finally let's check if it is a string.
So if input is a string then we are just outputting that it is a string.
And maybe let's cast it to uppercase.
So we use the str to upper function On the input.
And the default case would be we just output unknown type.
Now that is an interesting use case for the match expression because we are matching true and we want
to match it to a case that will also return true.
Now all of those cases can't return true because the argument can't be integer, float, and string
at the same time, or it can't be none of those at the same time.
So it's actually only possible to match one of those cases to true.
So that just might be an not really obvious use case for the match expression.
Okay.
So now let's use this function for something.
Let's define an array with different values.
So an integer float value a string.
Another integer, maybe another float value, and finally a string.
And then let's use a loop to go over all the elements.
That's inputs as input.
And for every single one of those elements from the array, let's just echo the output of process input,
passing the input.
And let's just concatenate that with a new line.
And I think it's time to run it.
I run the union PHP and this is our output, which basically just confirms that this function can work
with union types.
And also as a summary.
That's just the way you define union types.
If obviously you have a need for that.