Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (1)

0 answers

Post by Donald Isod on September 3, 2012

good job on these tutorials mate , helped me pick up the basic of PHP really fast

Type Casting

Type Casting

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:12
    • Lesson Overview
  • Type Juggling 0:54
    • Type Juggling
    • Automatic Conversion
  • Type Casting 3:53
    • Type Casting
    • Implicit and Explicit Type Casting
    • Explicitly 'cast' a Variable Example
  • Type Casting (cont.) 6:48
    • PHP Allows the Following Explicit Type Casts
    • The settype () Function
  • Type Casting Coding Example 9:00
    • Explicit Type Casts
  • String Conversions 14:52
    • String Conversions
    • Common Conversions to String
  • Numeric Conversions 18:18
    • Numeric Conversions
  • Boolean Conversions 20:29
    • Boolean Conversions
  • Homework Challenge 21:38
    • Homework Challenge

Transcription: Type Casting

Hello, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be discussing the topic of type casting, which is a way of converting variables of one data type to another data type.0004

In today's lesson, we are going to talk about a couple of things.0014

We are going to first talk about a term called type juggling, which is something that PHP does,0017

which is a way it automatically converts variables of one particular data type to another.0023

We are going to talk about type casting itself, which is the actual process of doing that.0031

And then, we are going to talk about a couple of different common conversions, like when you cast or convert a variable of one data type to another.0034

Certain rules apply, and certain things occur; and we are going to talk about some common cases0043

for strings, for some arithmetic operations, and for converting things to boolean values.0048

Type juggling--what does that mean? Well, PHP is known as a weakly-typed language.0056

What that means is that, when you declare a variable in PHP, you don't have to give it an explicit type.0061

So, when we do a=1, we are not saying to PHP that a is an integer variable.0066

PHP determines the variable's type from its context; so if we assign it an integer, it is going to be an integer variable.0077

If we assign a variable a floating-point number, PHP knows that b is a floating-point number.0084

This is different from strongly-typed languages, where, for example, in another language, you might...0091

if you want to declare a to hold an integer, you have to preface it with, for example, the int keyword, which says that a is a data type integer.0096

Now, the thing about PHP is: because it's weakly typed, a variable can also change data types.0106

Whereas, in other languages, you might only be able to assign integers to a variable that is declared an integer,0113

in PHP, if you have a=1, this statement will say "a is an integer that is equal to the value 1"; we can, in the very next line,0119

say a=2.5, and we have just implicitly changed the data type of the variable a.0132

PHP now knows that a is a float data type; and it does that on its own.0139

And because, in languages, operations and functions require data of certain types to work (for example, numeric operators require numbers;0144

the string concatenation operator requires strings to be its operands)--because they require that, 0155

PHP does what is known as automatic type conversion.0162

In other languages that aren't weakly-typed, you have to explicitly use...for example, if you are adding integers, 0166

you have to explicitly use two variables that are of the data type integer.0174

In PHP, it does that for you automatically.0179

If you try to add two numbers, and they are not both integers, PHP does an automatic conversion for you.0182

We are going to learn a little bit more about the specifics of numeric conversions that it does.0187

For example, when we add two numbers, it requires them to be numbers.0193

If you provide, in PHP, two variables to an addition operator that are not numbers, 0196

PHP is automatically going to convert them for you to the appropriate type, and then perform the operation.0202

This automatic conversion is known as what the lesson is titled today, type casting.0208

That is converting data from one data type to another.0213

Because this happens automatically in PHP, PHP has certain rules that it follows when it does this.0218

They may not be that obvious, so it is important that you understand the automatic casting rules that PHP uses,0223

so that you can get expected results.0230

Type casting, as mentioned, is the process of changing a variable from one data type to another data type.0235

This can be either done implicitly, as in automatic type conversion, where PHP converts the data type for you, 0241

or you can do it explicitly, where you can explicitly say, "I have a variable that is an integer; I want to make it a float."0250

There are two ways to explicitly cast a variable from one data type to another.0257

You can use parentheses syntax, which we are going to talk about in a second, or the settype function.0262

For example, if we have the variable here int1 that is set equal to the integer literal 1, and we want to make that a float variable,0268

what you can do is: we can create a new variable called float1, and what we do is set it equal to the integer variable name.0277

And then, what we do is: we have parentheses with the word float in between them.0285

What that means is: this parentheses segment right here says, "Take the value of int1, convert it to a float, and then assign it to float1 over here."0291

settype does a similar thing; the way settype works is: it is a function that has two arguments, 0303

which we are going to learn more about when we learn more about functions.0310

But for right now, you can just know that you write settype and then a pair of open and closed parentheses.0312

And then, you pass it two pieces of data: you pass it the variable you want to cast (in this case, we are going to cast the variable num),0318

and then you pass in a string that says what you want to cast it to.0326

So, what this operation is going to do is cast the variable num, which maybe is an integer, to a boolean.0330

It is going to change it from a number data type, like an integer data type, to a boolean data type.0338

Now, one thing to note is that, when you use parentheses casting, as up here, 0343

it doesn't permanently change the data type of the variable that it's casting.0348

So, when this operation here completes (I'll clean this up a little bit)--when this statement right here completes--int1 is still going to be an integer.0353

It's just that its value was converted to a float before it was assigned to float1; but int1 is still an int.0364

If you use the settype function to explicitly cast it, it changes the type of the variable specified.0370

So, this is essentially the same as if we had used parentheses form.0376

Whereas, up here, the variable to the right of the cast in parentheses--its data type doesn't get changed; 0390

down here, because we are assigning it back into the same variable name, its data type does get changed.0400

That is what the settype function does.0405

What are the different casts that you can do in PHP?0409

Well, you can cast between any of the different data types; you can change all of the data types from one to another.0411

Using the parentheses syntax, there are two ways to cast to an integer.0418

You can use int or integer between parentheses, and you just put that before the variable that you want to cast.0421

For float, you can use the word float, double, or real; and those are sort of legacy terms that the floating-point numbers used to be referred to as.0428

But the most typical one you will see will be float.0438

To cast to a string, you just put the word string in parentheses.0440

And just so you know, all of these are unquoted; these aren't previous strings--these are keywords in PHP.0444

So, PHP knows to interpret that as that this is a cast.0450

And then, to cast to boolean, you simply do boolean, or bool.0454

To cast to null, you put the word unset in between parentheses, which is kind of like the unset function we talked about in the lesson on the null data type.0459

The reason it wouldn't normally be null is because we can't use this, because null is another keyword in PHP.0469

As we know, it is the literal value for the null data type; which is why you use unset to cast the variable to null.0479

Something we are not going to really deal with that much, but I'll mention for the sake of completeness, is: 0488

you can also cast things to an array, using the word array, and to an object.0494

And the settype function allows all of these same casts; it just uses a different format.0500

So, if we want to cast to string, for example, you would type the function settype, and then we would put the word string in quotation marks,0503

because this function actually needs a second argument to be a string.0519

And in this case, it's a string set to the value string; but we also could...maybe we want to cast string, and we want to cast it to an integer.0526

So, we could put int in it, as well.0537

Let's take a look at some PHP code that uses these in action.0541

Here are some explicit tasks using the parentheses syntax.0547

Here we have a float variable called float1 that we are assigning the literal 1.25.0550

Then, we are going to create an integer variable; its value is the float1 variable's value, cast to an integer.0556

So, it's going to take 1.25, cast it to an integer, and set it equal to int1.0565

And, as mentioned, parentheses syntax doesn't touch the data type of float1; float1 is still a float data type with value 1.25.0571

However, int1 is going to be of the int data type.0582

Then, we go ahead and do another cast: we cast int1, which is an integer, to a string, using the parentheses syntax with the word string in between them.0585

And then, we convert the string to a boolean, using the boolean cast, when we simply put bool in parentheses.0593

If we look at the output of this script, we can see the parentheses syntax we have.0602

We declared a variable float, because it's 1.25; and then we run the var_dump function on that variable float1.0610

And the var_dump, again, outputs the type and value of a variable--it says it's a float variable that is equal to 1.25.0617

Now, in the other line of code, where we have created an integer variable which is the value of the float variable passed to an integer,0625

we can see that int1 is an integer data type, and its value is 1.0633

So, as you can see, one thing you might notice is that it stripped off the .25, because integers are whole numbers, and they can't have fractional parts.0639

When you cast a floating-point number to an integer, it actually strips off the fractional part.0647

And so, that is something important to notice.0654

Also notice, though, that, because this was a parentheses cast, it doesn't change the type of the variable to the right of it.0656

And so, float1 is still a float variable with value 1.25.0664

Now, we are going to go ahead and have int1, which is equal to the value of 1, and we are going to cast it to a string.0669

And we can see down here that that makes this string1 variable of the string data type.0675

And in this case, the var_dump for strings outputs the length of the string, which is one character.0681

Here, what it has done is just taken the number 1 and converted it to a text string, which is just the text representation of the number 1.0688

There is a difference between the number 1, just flat-out, and then the number 1 in quotation marks.0697

The number 1 in quotation marks is a string, and it is treated as such.0705

The number 1, as an integer or a float value, is something you can do arithmetic operations on.0708

And the string version, you can do string operations, like concatenate and stuff.0716

So, that is where data types do become important, in that you might look at the 1 as being the same thing...0719

There is a 1 here and a 1 up here, but they are not quite the same.0726

Then, finally, we did a boolean cast of the string; and we can see that the boolean one has become a bool data type, and its value is true.0731

And if you notice, the string1 is still a string, and it still has the same value.0741

And so, what happened is: when you cast a string value (we are going to learn more about this in a couple upcoming slides),0746

to a boolean, it has certain rules to decide whether it becomes true or false; and the number 1 ends up being true.0756

And so, there are a lot of different rules when it comes to type casting, and it's somewhat complicated.0765

There are a lot of different things to go over, and so, at the end of the lesson, I'm going to have a link to a page on the PHP website0772

where you can read more about it, because there are many, many different scenarios of how casting from one type to another--what actually happens.0779

As mentioned, parentheses syntax does not change the type of the value operated on.0789

It only changes the data type of the value for the assignment operation.0794

If we go back and look at the code again, there is a section created for explicit casts using the settype function.0801

I have done the same kind of thing we have done up here, with the same variables and the same values.0809

We have a float equal to 1.25 that we have cast to an int; then, we have an int equal to 1 that we have cast to a string.0812

A string, which is the text string 1, or the character string, which is just the text character 1--we are converting that to a bool.0820

What we are going to see when we look at it is that, unlike the parentheses syntax, it changes the type of the variable that is passed to the function.0829

So here, we have a float that equals 1.25; when we pass it to the settype function, and we pass it this string parameter,0841

that says int, which means "cast it to an int," float1 now is actually an integer, and it is an integer value of 1.0848

So, the actual data type of float1 changed.0856

Likewise, for all of the other different conversions...when we convert int1 to a string, 0861

int1 becomes a string data type with a string which is the number 1.0865

And then, down here, if we have a string that is (I don't know why this is unclear, but it is) the number 1 in between double quotation marks,0871

when we cast that variable to a boolean type, string1 becomes a boolean data type that is true.0878

And so, as mentioned, settype permanently changes the type of the variable that you pass to it when it does a cast.0885

So now, I want to talk about a couple of common conversions.0895

There are many different rules, as I mentioned, that happen when converting from one data type to another.0897

We can't go over all of them, but I'm going to talk about a couple of the more common ones.0902

One of the most common conversions is converting things to strings.0906

And actually, we have already been doing this in our code.0909

We haven't explicitly talked about it, but any time you use double-quoted syntax and variable interpolation,0912

where you have a variable within double-quoted strings, PHP is converting the value of this variable to a string data type.0917

For example, if we had a=1, and then we echoed the variable a in between parentheses,0932

what PHP is going to do is: it is going to go; it is going to get the value of the variable a, which is the integer 1;0943

it is going to cast it to the string data type, because echo only knows how to print out strings;0949

and then, after that cast, then it outputs it.0953

So, some of the common conversions that happen to strings are: if you have an integer or a float, when you convert them to a string0956

(which you have seen in some of the code that we have done, like in our store web application, for example,0965

like the calculation of the price), it takes the number, and it just converts it to its text version.0970

So, 14 becomes the string 14; if we have a float value, like 2.5, that just becomes a string, 2.5.0975

That is a pretty intuitive conversion; some of the less intuitive ones are--if we convert the boolean value true to a string, it becomes the value 1.0989

If we convert a boolean value false, it becomes the empty string, which is just a string with no characters in it.1000

And if you convert null to a string, it also becomes the empty string.1008

For example, let's say we had a boolean variable bool1=true, and then we wanted to output that.1013

We did echo "$bool1"; what it is actually going to output is: it is going to take bool1; it is going to see that it is the value true.1031

It is going to convert it to a string, which, as we can see over here, is 1.1044

So, the output of this is going to be just the string 1.1048

Now, one thing to note here is that, in this echo statement, I have enclosed the variable in double quotation marks.1056

And typically, you wouldn't do that; you would just write echo and then the variable name.1063

You would use the double-quoted syntax when you had other things that you were including in this string here.1070

Like you might say "bool is equal to___" and then output the variable's value.1075

But when you use it in this way, as well, without the double-quoted syntax, because echo works on string values,1079

it is going to implicitly cast this bool data type to a string, which, in this case, becomes 1.1087

This is also going to echo out; it is going to echo out the string 1.1092

There are also some numeric conversions that happen; we talked about one earlier.1099

One of the most important ones is when you convert a float to an integer; it rounds down to 0--it just basically strips off the fractional part.1105

So, if you have a number 1.76, and then you were to cast it, let's say, to an int, then its value would now just be 1.1112

That is something important to note.1123

Now, also, the addition, subtraction, multiplication, and division operators--they all require float operands to operate.1125

So, when you have an addition operation or a multiplication operation--any of these--it takes both the operands,1136

and it converts them to floats, and then performs the operation.1142

Now, because, for example, it is going from int to float, the only other type of number that we would have would be an integer.1148

When we convert that to float, it simply becomes the same number; so 1 as an integer would just become 1.0 as a float.1154

That doesn't really cause any problems, whereas going from float to integer, you end up losing information.1164

So anyway, these operations all convert both of their operands to float values, perform the operation, and then return a float value.1171

The only exception to this is when you have two integers that are evenly divisible; then, it actually returns an integer.1183

The other thing to note, as far as automatic arithmetic conversions go, is using the modulus operator.1192

The modulus operator converts both of its operands to integers before it performs its operations.1199

So, if you had something like 7.5 mod 3.0, PHP would convert them both to integers, so this would actually end up being...1205

We know, when you convert a float to an integer, it strips off the fractional part, so it would become 7 mod 3, which is equal to 1.1218

I want to finish up with talking about some boolean conversions and different rules that apply when you convert certain values to a boolean.1230

Basically, as far as booleans go, any value that you cast to the boolean data type is going to be true, except for these cases right here.1240

If you cast the integer 0 to a boolean, it is going to end up being the boolean value false--1251

likewise, for the float value 0, the empty string (which is just a string with no characters), or a string containing just the number 0, and also null.1259

So, if we were to cast boolean null, the value of a would be false.1269

The rule with boolean conversions is: anything except for these values is going to be interpreted as true.1288

For the homework challenge today, I would like you to go and look at this link on the PHP Manual website.1299

Explore some of the links on this page: it's called Type Juggling.1305

It also has a section in it called Type Casting, which is the heart of the material.1308

Just kind of explore around, because there are a lot of different rules that apply when casting from one data type to another.1314

There are a lot of different intricacies when you go from floats to ints, and then especially...we talked about some of the more common ones:1318

converting floats to ints, converting numbers to strings...1324

But then, you can also do things like convert strings to numbers, convert numbers to booleans, and so forth.1329

And there are all sorts of different rules that apply, and so, in order to know exactly 1335

what PHP is going to be doing when you use these operations, you have to refer to the website1339

to find out all of the different things that happen.1344

And the rules aren't too many, but they are somewhat complicated, and so it would be a good idea to go and review them before you start using them.1347

That ends today's lesson; thank you for watching look forward to seeing you at the next lesson.1356