‫All right.
‫So when dealing with types, there may be times when you need to convert a variable from one type to
‫another.
‫And this is called type casting.
‫And PHP has two ways to do this.
‫Or two situations where this can happen.
‫The first is called implicit type casting.
‫This is when PHP automatically converts a variable from one type to another in certain situations,
‫this is also called type juggling, and then the second is explicit type casting.
‫This is when you explicitly tell PHP to convert a variable from one type to another, whether it's a
‫string to an integer or an integer, a boolean to an integer, whatever it might be.
‫Now, for beginners, I just want to explain that this isn't something that you're going to deal with
‫a lot.
‫I just want you to know how types can be changed, both by PHP and by you as the developer.
‫So if this seems confusing at this point, that's fine.
‫I just thought this was a good place to discuss this since we're talking about types.
‫So first we're going to look at implicit types, which means that PHP changes the type on its own.
‫And there's a few situations where this can arise.
‫So I'm going to open up a PHP tag here.
‫And I'm going to create a couple variables.
‫So let's say number one I'm going to set that to the integer five.
‫Let's say number two I'll set that to the int of ten.
‫And let's say number three.
‫And I'm going to set that to a string of 20.
‫So I'm going to put quotes around that one now instead of just, you know echoing out a bunch of stuff.
‫What I'm going to do is just put the result of whatever I'm trying to show you into a variable called
‫result.
‫So I want to add these two together, number one and number two.
‫So let's say number one plus.
‫Number two.
‫And then at the bottom here I'm going to var dump the result so we can see what that gives us.
‫And let me just put a comment here and we'll say implicit.
‫So this is implicit conversion, meaning that we're not we're not doing it.
‫PHP is.
‫So in this case if I load this up we're going to get int.
‫And the reason we got int is because number one is an int.
‫Number two is an int.
‫And we're adding them together.
‫So the result that we get is also going to be an int.
‫So very simple.
‫Now what if we add an int with a string.
‫So I'm just going to overwrite that result variable.
‫And let's take number one.
‫But let's add to it number three which is a string.
‫What do you think we're going to get here.
‫So we'll save that reload and we get an INT.
‫So what happened here was a string to integer conversion.
‫So I'll just put let's put a comment here.
‫So this is going to be an int.
‫And it's a string to int.
‫Because number three started as a string.
‫But since we're we're trying to add it here PHP said well it is a number inside of the string.
‫So let's make it an integer okay.
‫Let's make it a number and then add it.
‫Added them together.
‫That's why it's if it was looked at as a string, then it would have concatenated it.
‫And the answer would not be 25.
‫It would be 520 because it would just concatenate 5 to 20.
‫But in this case PHP will convert it into an int.
‫So what if we do two strings that have numbers inside of them?
‫Let's say the result is going to be number three.
‫We'll say number three plus number three.
‫So let's see what happens here.
‫Is it going to be a string or an int.
‫So it's an int right.
‫So basically it converted this 20 to an int here and here.
‫And then added it together.
‫And we ended up with an int.
‫And again we did a string to int conversion.
‫Okay for this one we'll just say int.
‫Now, if you do want to concatenate like in JavaScript, this is the concatenation symbol.
‫So our characters.
‫So we could use that.
‫But in PHP again concatenation we do that with a period.
‫So we could say number three.
‫And we want to concatenate actually let's do number one because that's an int.
‫And then we're going to concatenate onto that number two which is also an int.
‫And let's see what that gives us.
‫That gives us a string of 510.
‫It took five even though we we created it as an int since we used the concatenation symbol here, it
‫took five and it concatenated ten onto it.
‫So in this case this is a string.
‫And it did int to string.
‫Now let's try something a little weird.
‫Let's have a variable called fruit, and let's set that to a string of apple.
‫And then we're going to come down here and let's say result and let's set that to fruit.
‫And let's try to add an int to it.
‫We'll add number to.
‫So what do you think that's going to give us?
‫Apple plus ten.
‫So we'll reload and we actually get an error.
‫It says unsupported operand types string plus int.
‫So it looked at the string and it saw that it had Apple in it.
‫And it said what the hell do I do with this?
‫I can't make this a number.
‫So it actually just throws an error.
‫So let's just say error here.
‫And you're going to want to comment that out, because anything you put under here isn't going to show.
‫It's just going to show this error.
‫It's a fatal error.
‫So it stops the script.
‫So just comment that out.
‫Now booleans are interesting.
‫So let's say actually before we do that let's come up here and let's add a couple more variables.
‫Let's say bool one.
‫Set that to true and we'll say bool two.
‫And we'll set that to false.
‫And then what I want to do is for the result, I'm going to take a number.
‫So number one which is five.
‫And I'm going to add on to that that bull one which is true.
‫So we're saying what is five plus true.
‫Let's see what we get.
‫So we get an int right.
‫And the the value is six.
‫So five plus true is six.
‫The reason for that is when PHP looks at true and it sees we're trying to add it to an int, true is
‫actually converted into a one.
‫So that's why we get the answer of six.
‫We're actually doing five plus one.
‫So in this case we're getting an INT and we're saying we're getting bool to int conversion.
‫So let's also do number one plus bool two which is false.
‫So here we're doing five plus false.
‫Let's see what we get.
‫Again we get an int but the value is five because false actually gets converted into a zero.
‫So five plus zero is five.
‫But it's doing that bool to int conversion.
‫All right.
‫And then the last thing I want to do is try null.
‫So let's create another variable here.
‫We'll say variable called null.
‫Set that to null.
‫And what do you guys think is going to happen here if we say number one plus?
‫Now let's go ahead and save and reload.
‫And again we get int five because null is being converted to zero.
‫So false and null will be converted to the int of zero.
‫True will be converted to the int of one.
‫In the case where we're using it with an integer.
‫All right.
‫So in this case we're saying null to int.
‫So that's implicit conversion.
‫And there's there's some other situations where you might find PHP changing types as well.
‫But we're going to look at explicit conversion.
‫Now there might be a reason why you want to change a variable to something else.
‫So let's say result.
‫And let's say I want to take number one which is five an integer.
‫And I want to change it to a string for some reason.
‫So what I could do is I could just prefix it in parentheses, I could say string.
‫And then number one which is an int.
‫And let's save that.
‫We're going to vardump what that gives us.
‫And we get a string of five.
‫So it changed this int of five to a string of five.
‫And we could do the same thing with an int.
‫Actually I'll copy that down.
‫And let's say in here instead of string we'll say int.
‫And we know number three is a string.
‫So let's change that to number three.
‫And now we get an int.
‫So it took this string of 20in the number three variable and changed it to an int.
‫All right.
‫We can also change to a ball.
‫So let's say result.
‫Equals.
‫And let's say we want bull.
‫And we'll use number one which is five, and let's see what that gives us.
‫So that just gives us a bool of true.
‫Now, if that number is a zero, let's do that and reload.
‫Then we get false.
‫Okay, so any number aside from zero when you convert is going to be true if you're converting it to
‫a boolean.
‫However if it's zero then it's going to get converted to false.
‫All right.
‫So again this isn't something that you're going to do very often.
‫But it's just good to know how how this stuff works.