Transcript for:
Types and Typecasting in PHP

[Music] let's talk about the types and typecasting in php php is dynamically typed or also known as weekly typed language where you are not required to define the type of your variable and also the type of the variable can change after it has been defined dynamically typed language means that the type checking happens at run time while statically typed language means that type checking happens at compile time for example java c plus plus and c sharp are the statically typed languages because php allows such type system it is more flexible but that flexibility comes at a price of performance and can sometimes result in unexpected bugs however php has improved its type system a lot in latest versions and it even supports strict types php supports 10 primitive types which are grouped by four scalar types four compound types and two special types and there are also two pseudotypes which are mainly used for readability these are mixed and void but don't worry about these two for now i will briefly talk about these types in this video but i will also dedicate a video to each of the types separately and the reason for that is because i want to go over each of these types in more detail where i could discuss some very important points and also putting all these in a single video would make the video too long so let's review what these types are the four scalar types are boolean integer float and string boolean is just a simple representation of a truth value it can either be true or false integers are the numeric values without the decimal points also known as the whole numbers so examples could be something like 1 2 3 0 negative 5 and so on floats or floating point numbers also known as doubles or decimals are the numeric values with the decimal points so examples of floats would be 1.5 0.1 0.005 negative 15.8 and so on and we've already touched strings in the previous videos and these are just a series of characters that are enclosed within either single or double quotes so this could be geo hello world and so on let's define some of these types as variables and print them out on screen so let's go ahead and define a variable called completed and set that to true let's define a variable called score and set that to 75 let's define a variable called price and set that to 99 cents and let's define a variable called greeting and say hello geo so let's print each one of them out in a new line i'm going to add the br html element here so that we can see everything on separate lines let's refresh the page and we see 1 75 99 cents and hello jail now these three make sense the first one doesn't really make sense because we assign the value of true but it's printing as one we'll talk about this more in a separate video dedicated to the boolean types but just keep in mind that when printing out true it will be printed as one and when printing out false it will be printed as blank so false would be printed as nothing also this does not really indicate the type of a variable now there are a couple ways you could find out the type of a variable there is something called get type function and you could pass it the variable and it will return the type of the variable so for example we can do echo get type and do completed and that will return boolean we can do score and they will return integer we can do it for price and we'll return double which is the same as float we can do greeting and that will return string another way of finding out the type of the variable is by using something called var dump and then you provide the variable so completed in this case and vardon basically just prints everything he knows about the expression you give it to so we gave the completed variable here and if we refresh the page it prints the value that the variable holds which is true in this case and it also prints the data type of that variable which is boolean in this case if we var dump the score we see that the value is 75 and its data type is integer let's far down price the data type is float and value is 0.99 and let's far dump the grading and that's data type string with nine characters and this is the value so these are the basics of the four scalar types as i mentioned before i'm going to go into more details on each of the data types because there are some important information that you need to know how these values actually get casted into different types and how they're handled by php arrays are basically just a list of items and these items can be of many different types for example you could have list of integers you could have list of booleans you could have list of mixed booleans and integers and floats and so on we'll talk about the types of arrays and more details about arrays in separate dedicated video so we can define a variable called companies and set that to square brackets and this indicates that this is just an empty array you could put values in here so we can put something like one two and three and then we can also put floating numbers 0.5 negative 9.2 we can also put strings in it and we can put booleans as well so as you can see the array is just list of items and how do you print arrays if you do echo companies it is just going to print the word array and you're going to get a notice that php is trying to convert an array into string and it's kind of failing because it does not know how to convert an array into a string and it's just displaying the word array now on your computer this might be hidden depending on what the error reporting is set on your php configuration file we're going to talk about the php configuration file in a separate video and how to set different error reporting levels and so on so don't worry about that for now another way you could print arrays is by using a function called print underscore r and as you can see it just prints that already in more readable format i'm not gonna go into more detail about it in this video because i'm going to have a separate dedicated video to erase and i'm going to discuss all the necessary details in there and the last three compound types objects callables and iterables are a bit advanced types so i don't want to overwhelm you with too much information in this video but i will definitely have dedicated videos on each of these types don't worry we'll get there and you will know what these types are and how to use them and finally we have the two special types one is resource and the other one is no for the resource same applies as to these three i'm going to have a separate video about it so don't worry about it now and no it just simply means no value or nothing so now you know the basics of the data types in php how does actually php know that this variable for example is an integer we have not specifically stated that this variable is an integer we just simply assign the value 75 to the variable of score and when we do var dump of the score we get integer and this happens because php will automatically determine the data type during runtime and in this case php will basically figure it out that you're trying to define this variable as an integer because it has no quotes around it if we were to put quotes around it then php will figure this out that this is not an integer this is actually a string and if we do var dump as you can see it will print 75 with the data type of string there is also the concept of type hinting within your functions and class properties i will cover the functions and class properties in more detail within the separate videos just bear with me here i just want to show you an example of what type hinting is and how it works and i'm going to create a function that will sum two variables and return the value so we're going to call function sum they will accept two parameters x and y and return x plus y now don't worry about class operator either or the return type we'll cover all this later and we're simply going to echo out the value of this function call so we're going to call sum on the numbers 2 and 3. and if we refresh the page that returns 5. so it's working as expected now again php dynamically assigns the data types to these variables if we do var dump here for x and y these are coming up as integers but what if you passed something like a string instead of an integer what would happen in that case as you can see it's taking the integer and then string and it's returning the integer five and we know that it's an integer because we can just assign this to a variable and then echo out sum and then we can also var dump the sum right here and we see that the value is integer let's add some line break here for clarity and also add line break here you could specify that the variables x and y are integers and now when we refresh the page the second variable is coming up as integer and not as string even though we have passed the string in here and this is called type juggling or type coercion basically what's happening is that even though we have type hinted that this variable must be integer php will still try and convert the given value to the function to the appropriate data type so this means that the type of the variable is determined by the context in which the variable is used now you could also do something like pass 2.5 as a float and even though we're expecting integer this will convert this into number two and we get the exact same result now if in case php cannot do this conversion dynamically then it will throw an error for example if we expected something like an array and refresh the page we get the fatal error also it's worth mentioning that the type of a variable is only guaranteed to be that type up until that point so what i mean is that we are guaranteed that this is going to be integer only up until this point after that it can change because you could technically put here x equals 5.5 and this will convert x into a floating number and that is perfectly acceptable in php so for example if we change this back to an integer and we change this back to three and we refresh page here we get 5.5 as float because we are overriding right here to 5.5 so as mentioned before don't worry about this too much we'll talk more about this in more detail in upcoming videos i just wanted to cover this in a basic level so that you have an idea how it actually works now there is a way to enable strict mode on php and the strict mode basically means that you will throw an error if you pass another type than integer even if you pass something that can be converted dynamically so let me show you what i mean i'm going to remove these and let's make sure that it still works and it does and let's change this to string and right now it still works and now let's enable the strict type and you can enable that by using declare strict types equals one and right now as soon as i added that my id is already underlining this here and telling me that string is not an acceptable type it's expecting integer and if we refresh the page here we're getting fatal error however there is one exception here even in strict mode you can still pass an integer where a float type is expected without any error so for example let's change these expected types from integer to floats and let's pass two floating numbers 3.5 and 2.5 and this is going to work now let's pass two integers and as you can see id is not underlining and if we refresh this works as well so should you use strict types and type hinting it is entirely up to you though i personally recommend it and i personally use type hinting and streak types as much as i can it provides better quality of code where i know exactly what types are accepted and it will avoid unexpected bugs so let's talk about typecasting so let me clear these out and let's define a variable here x equals five and let's put that in strings let's do var dump x and we get value of five in a string data type you could actually force this to be an integer type and you could cast anything to the data types using the name of the data type within the parentheses so if you want to cast this into an integer you open parenthesis you type in or integer and you close the parenthesis so this would now cast the string five into an integer five and if you refresh that that works i'm going to leave some useful links in the description about types and casting so please check them out if you want to know more thank you for watching please hit like and subscribe and i'll see you on the next one