6th PHP Tutorial Changing Data type in PHP

Last Updated on

Data Type in PHP

In this PHP tutorial we will learn about data types how to change data type in PHP. There are many types of data type we use in programming.

For example in numeric data type we use ( 0 to 9 ) digits and for alphabetic data type ( A to Z ) characters are used. But in alphanumeric data type we can use numeric and alphabetic characters.

When create a variable we have to define its data type to place it in right configurations in computer memory. If we use variables without data types it may cause errors while executing.

While working in PHP the data type automatically assigned when we store value or data to a variable.




Changing Data type using Casting

Often in programming while writing script we have to change the data type we can use casting functions for this purpose.

When using casting for changing in data type it will create a new variable with new changed data type:




$var2= (double) $var1

Echo gettype($var2)


Code understanding:



Here we created a variable by assigning value 3 we did not assigned variable type.

$var2= (double) $var1


In this line we created a variable var2 and assignend it var1 value with data type Double
So in this way we changed the variable data type.

Echo gettype($var2)


In this line of code we are getting outputs to check if variable data type is changed successfully or not.

We can use Echo or Print command to get output in the browser screen both commands are almost same.

READ  Introduction to Web development

As we know we always need to create and use variables while programming or writing any script.

o we should have basic knowledge and strong gripe over using variables. Variables are most important in every programming language so we suggest you strongly to make practice and repeat process using other variable types.

Like String, Integer, Boolean, we will also take a close and deep look to variables and data types in next tutorials. Also we will create a complete PHP project to understand better.