How To Convert Data Types In PHP

Convert Data Types in PHPWe have already discussed about different types of PHP data types such as Boolean, Integer, String, Float, Array and Object. If you need to convert data types from one type to another type, it is possible in PHP. There are different methods to convert one data type to another data type. You can convert PHP data types either manually  using type casting operators, using type related  functions or automatically with type juggling.

Ways To Convert Data Types In PHP

You can convert data types from one type to another using type casting or type juggling. Type casting is the manual conversion from one data type to another manually using type casting operators. Whereas, type juggling is the automatic conversion performed based on the circumstances.

Converting Data Types Using Type Casting

Type casting can be achieved by placing the intended types in front of the variable to be cast. The different type casting operators that can be used to convert the variable from one type to another are given below.

cast operators → conversion

(array) → array

(bool) or (boolean) → boolean

(int) or (integer) → integer

(object) → object

(real) or (double) or (float) →  float

(string) →  string

Here are the some examples of converting one data type to another using type casting.

The expression $number=(float)16; converts the integer 16 to the float 16.0

The expression $marks=(int)11.21; converts the floating point number to the integer 11.

Converting Data Types Using Type Juggling

Type juggling is the feature of PHP where the variables are automatically cast to best fit data type in the circumstances while manipulating with mathematical operators.

Type juggling converts automatically to the upper level data type that supports for the calculation. Following is the example that converts string into integer while adding to the integer.

<?php

$sum=15; //an integer

$increase=”10″; // a string

$sum+=$increase; // $total=20, which is an integer.

?>

Here is an another example that shows the PHP’s type juggling capabilities. Hence, the number used in string can be used to calculate with the integer.

<?php

$theory=45; //an integer

$practical=”15 is the number obtained”;    // a string

$sum=$theory+$practical;      // $total=20, which is an integer.

echo “The Total Number Obtained is ” .$sum;

?>

Output:

The following output will be generated from the above PHP code.

The Total Number Obtained is 60

PHP Type Related Functions

In order to convert from one data type to another, you can also use some type related functions. Each functions are available either for verifying or converting data types.

To Retrieve Data Types

Since, it is possible to retrieve data type of any variable using function. So, you can use gettype() function that returns the type of the provided variable.

Hence, this function can return the values for array, Boolean, double, integer, object, string and unknown data types. Following is the syntax for retrieving data type of any variable.

string gettype(mixed var)

Here is an example that returns the data type of the variable $marks.

<?php

$marks=39;

echo “The Data Type of The Variable is “.gettype($marks);

?>

Output:

Following is the output generated from the PHP code written above.

The Data Type of The Variable is integer

To Convert Data Types

There also a builtin function in PHP that can convert data types from one type to another. The function settype() converts a variable to the data type as specified. So, the possible data types you can convert are array, Boolean, float, integer, null, object and string. While converting, the successful conversion returns the Boolean value True, otherwise returns Boolean value false.

Following is the syntax for converting data type using settype() function.

boolean settype(mixed var, string type)

Here is an example that sets the variable of integer data type to sting data type.

<?php

$total=159;

settype($total, string);

echo “The Data Type After Conversion “.gettype($total);

?>

Output:

Following is the output generated from the PHP code written above.

The Data Type After Conversion  string

Type Identifier Functions

Along with the gettype() function, PHP also provides type identifier functions for determining variables type. The functions such as is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar() and is_string() are type identifier functions used in PHP.

Type identifier functions return Boolean value True if the variable is such type, otherwise return False value. Following is the syntax of using type identifier functions.

boolean is_name(mixed var)

Following is an example of using type identifier functions while coding with PHP.

<?php

$var=19;

$type_int=is_integer($var);

echo “The Variable is Integer Data Type “.$type_int;

?>

Output:

The Variable is Integer Data Type 1

About shuseel

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 5 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.