Okay.
So now let's see some data types in PHP in practice.
And the first step is to create a types PHP file and then open it.
And let's see some examples.
So let's start by adding the PHP starting tag and define the is student variable.
But this time let's not associate a boolean value with it, but instead a number.
And also to output data.
We're not going to use the echo statement, but a var dump function.
So because we use vardump, we're not only going to see the value of the variable, but also its type.
Like in this example that's an integer and the value is one.
So next up something that you will often do with programming in general is you will make some conditions.
And depending on those conditions something else would happen.
So you will often check if the value of specific expression is either true or false.
Now, fortunately, with this Vardump function, we can output multiple values by calling it just once
by separating them with a comma.
So now I can compare the student variable using the comparison operator which is two equality signs
with true.
Now let me run that.
And the result of that is true.
Now we see that if student is an integer and we are comparing it with true.
Now if I would also dump the value of true, we can see that this is of type boolean.
So those are different types.
And what happened here is again type coercion.
So PHP is trying to convert the types.
So the whole expression here makes sense so that the values can be compared.
Now you should know that in PHP every number that's non-zero will just evaluate to true.
And this is often used.
So if I for example check with minus one then this comparison is also true.
But if is student is set to zero this is then false.
Okay so let me change the value here to one.
Now here in this comparison let's use a strict equality operator which is not two equal signs but three
equal signs together.
And let's run that.
So as we see the result of this comparison is now false.
And nothing else has changed.
So that's another way of comparing values in PHP that just takes into account also the types of the
two expressions on the left and on the right side.
So with just two equality signs, the type coercion will happen.
But if you use the strict equality operator, then you are also making sure that the two values, the
two expressions are of the same exact type, and only then you verify if the value is identical.
Okay, so next up let's see an example with an array.
Let's define a variable called scores and an array starts with square brackets.
And let's just put some values in there.
This would be number 85 an integer.
But obviously we want to make things complex.
So the next value would be a string 90.
Okay, now let's vardump the result of just summing up all those values.
So the way you access elements in the array is if that's an indexed array and it is an indexed array
I just use the indexes starting from zero.
So I'm gonna just add the scores zero to scores index one.
And as you see the result is 175.
So the types were coerced or this is sometimes also called type juggling.
So the types were coerced to an integer.
Okay.
So let's make it even more interesting.
Let's add another value 95 and half.
That is a float value floating point number.
So let's add this.
The index here would be two.
So as I said we index from zero.
Let's see what will happen now.
So now the result is float.
So as we see there is some precedence in what type is being used.
If I remove this string and then I also need to get rid of this element.
Let's see what will happen.
So we also get the float as a result.
Now what's interesting if I surround this float value in quotes in double quotes.
So this is a string now and I add those two values I still get a float.
So what we see here is just PHP is trying to guess what's the best type that it should be using to calculate
something sensible from an expression.
Now we can also vardump the scores array.
So let's see how this would look like.
So this is how the array would be outputted.
We see that there is the index zero which contains an integer.
And at index one we've got a string.
So you can also influence the type of the value by typecasting.
So in this example here let's have an output.
Again we've got an array that has an integer.
And the string though the result is float because it makes sense according to PHP.
So I can do typecasting by using this int operator.
And now I'm gonna first convert this element to an integer.
Let's see what will happen then.
So now in the array we've got two integers.
This one and that one, so that the result of adding those is also an integer.
Now keep in mind that this way here is just a way to cast to another type that might not be necessarily
the best way to get an integer out of a floating point number, because you might want to apply different
rules.
And we're going to talk about that later on when I will talk more about numbers.
For now, let's also see another example.
This time we're going to use echo.
And maybe here I'm just going to move those scores to the total variable and change it that way.
So now that I can echo the value of total.
So this would be total score is.
And previously we have used the concatenation operator.
So dot.
So if I would like to uh sorry the name is Total.
If I would like to output the score, I can do something like this.
The total score is 180.
I can also add a new line here.
Like this.
But what we can also do, and that only works with the double quotes, is I can just directly put a
variable inside double quotes like this.
As you see, even the code editor is highlighting this as something separate.
And if I run this again, this also works.
Now keep in mind that you can't put variables like that inside single quotes, because this would just
output the name of the variable.
And also special characters don't work within single quotes.
So let's summarize.
So in this video we talked about the type coercion, and I think it's super important to just realize
how important concept this is, because it can really have some serious consequences.
What we did here, by comparing just this variable value to true, is something that you're gonna do
all the time, because those kind of comparisons are often used in control structures that decide the
flow of the program.
Now, if you wouldn't be aware of the implications of type coercion or just using the strict equality
operators, then well, you just might get into trouble.
So it's super important to just be aware of type coercion that we've also seen in this array example.
And then we've also seen how you can explicitly cast one type to another by using the typecasting operator.