In this article, you will understand the PHP type casting and how to get the type of variable values. So before getting started you have to know what are PHP data types.
Table Of Contents
Anyway, the PHP data types are ten primitive data types used to define the PHP variable values. Such as integer, string, float, boolean, array, object, and other types such as null, and resources.
And here, we need to cast these types into other types in PHP. And that can be done using the two operator braces and between them the new type and place the old value besides.
$variable = (New_Type) Old_Type_W_Value;
The following list shows you the primitive data types that can be converted to another.
- Integers
- Boolean
- Strings
- Floats
- Array
- Objects

PHP supports the predefined function to retrieve the data type of the target variable value, and that can be done using the gettype function.
PHP Get Type
The PHP get_type is a callback getting the type of any PHP variable or any returned data type. It takes only one parameter for the data value.
This callback is returning a string data type. Let’s see an example.
<?php
$var = 11.5;
echo gettype( $var ); // double
?>
Let’s focus on the casting for all PHP data types with examples.
Casting Data to PHP Integer Type
The PHP Int is a real number that can be assigned to a PHP variable. And to cast data type else to an integer value, you would use the following PHP code according to the previous type.
<?php
echo (int) "Value"; // 0
echo (int) ""; // 0
echo (int) null;// 0
echo (int) 1.5; // 1
echo (int) array(); // 0
echo (int) array("Hello"); // 1
echo (int) true; //1
echo (int) false; //0
?>
But there are some data that couldn’t able to cast to other data types, such as an object will never cast its data to an integer, let’s see that.
<?php
echo (int) new stdClass();
?>
In the previous casting example, the lexical analysis of the PHP interpreter will show you an error like the following.
PHP Warning: Object of class stdClass could not be converted to int in index.php on line ...
So to wrap this part up, the integer type can give you the following results when casting another data type to an integer.
- Converting a string value to an integer will show you a zero result.
- If the value is an object, it will show you a PHP warning error.
- The null value also shows you a zero value.
- When you cast a float value, it gives you a real number value, so deletes the decimal point.
- Converting an empty array will give you a zero value, but if the array has data it will show you “1”.
- When converting a boolean value, it will show “0” for “false” and “1” for “true”.
Anyway, in the following data type, I will expose the casting to the PHP string.
Casting Data to PHP String Type
As we explained in the PHP string. The string values mean a sequence of characters placed inside single or double quotes.
To cast another data value to a string, use the next code.
<?php
$var = (string) 1;
echo $var; // 1
var_dump( $var ); // string(1) "1"
echo gettype( $var ); // string
?>
Let’s convert the other data types to strings.
<?php
echo (string) 5.6; // 5.6
echo (string) true; // 1
echo (string) false; // => an empty result like this ""
?>
You will never be able to use the same way to convert an array or an object to a string data type. Because it will show you the following errors.
1- Casting an array to a string will show you this message.
PHP Warning: Array to string conversion in index.php on line .....
2- Casting an object to a string will also show you the following error.
PHP Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in index.php:5 Stack trace
But to convert an object or array to a string, you have to use the serialize() predefined PHP function. To see that, follow the next example.
<?php
class Car {
public $peed;
}
$obj = new Car();
echo serialize($obj);
echo "\n";
echo serialize( array ( "data", 1, true, 154.56 ) );
?>
The output would be like the following.
O:3:"Car":1:{s:4:"peed";N;} a:4:{i:0;s:4:"data";i:1;i:1;i:2;b:1;i:3;d:154.56;}
Anyway, let’s wrap up the string data molding in a few points.
- Casting an object or array to a string using the two braces method will never work, it only shows you an error.
- When you convert a boolean value to a string it will show you “1” for a true value and nothing for a false value, it is like an empty string “”.
So when you use the casting method for the string data type, it will show you the result inside quotations.
In the following part, I will explain how to convert a data type into a boolean type. Let’s dive right in.
Casting Data to PHP Boolean Type
The PHP boolean has only two values, true or false. And we can use it to detect database connections or check for a correct value.
We can use the same way as in the previous sections to cast any data type to the boolean. Let’s see how it does work.
<?php
$var = (bool) "Hello world"; //1
echo gettype( $var ); // Boolean
?>
Note that, when you print the value of the boolean type, it will never print the real value (true or false). It will show you 1 for true and nothing for false.
So in the following example, it will print a 1 or nothing value. Let’s see that.
<?php
echo (bool) null; //
echo (bool) "Hello World."; // 1
echo (bool) ""; //
echo (bool) 11.5; // 1
echo (bool) 51; //1
?>
But when you use the gettype() function, it will show you the new converted data type. Let’s see that.
<?php
$false = (bool) null; //
$true = (bool) "Hello World."; // 1
echo gettype( $false ); // Boolean
echo gettype( $true ); // Boolean
?>
In the array data type, the boolean is working according to the array contents, which can show you a true or false.
So if the converted array is an empty value it will show you a “false” as a boolean value, otherwise, with a filled array, it will show you a “true” value.
<?php
$array = array( "name", 55, 1.5, true, [] );
var_dump( (bool) $array); // bool(true)
var_dump( (bool) [] ); // bool(false)
?>
But what is happening when converting the object data type into the boolean?
The result is, that is no error happening here. It will print a true value.
<?php
$obj = new stdClass();
var_dump( (bool) $obj); // bool(true)
?>
Let’s wrap up this section.
- The integer data type can be converted to the boolean, and it will be a “true” if the result is up to “0” number or a negative number, otherwise, it will show a “false” if the converted was “0”.
- In the string, if the value was an empty result, it will show a “false” value. But if it is a real string, it will show you a “true” value.
- Arrays and objects are working with the boolean data type according to their contents.
- The float number also shows a true as a boolean value.
In the few lines, I am going to cover the casting of the array data type.
Casting Data to PHP Array Type
The PHP array is a predefined PHP function. It is a list that contains a lot of data values grouped by array key. The array can take this shape [] or this array() function.
<?php
$array = array();
$array_1 = [];
?>
Anyway, To cast another data type to the PHP array, you have to use the same way in the previous sections.
Casting the PHP string and PHP int to the PHP array.
<?php
$string = (array) "CodedTag.com";
print_r($string);
?>
The output would be like the following.
Array ( [0] => CodedTag.com )
So when you cast a real scalar data type into an array type, it will place this value as a field in the array. And it takes the zero index.
Let’s see the results when casting the other data types into an array.
<?php
$int = (array) 115;
print_r($int);
?>
The output would be like the following text.
Array ( [0] => 115 )
Casting the PHP Boolean to an Array.
<?php
$bool= (array) true;
print_r($bool);
?>
Also, the output will be the same as the previous PHP coding
Array ( [0] => 1 )
Also, you can cast the object data type into a PHP array using the following code
<?php
class Car {
public $speed = 200;
}
$array = ( array ) new Car();
var_dump( $array );
?>
The out of this array can be like the following.
array(1) { ["speed"]=> int(200) }
Let’s wrap up the data type casting to the array data type in a few points
- When converting a scalar data type to the array, it will place the scalar data type as a field inside the array, and it gives it the zero index.
- To cast the PHP object to an array, you have to use the braces way, and the result will be placed as the attributes as the array key and its value as the array value.
In the following part, I will explain how to convert the PHP data types into a PHP object.
Casting Data to PHP Object Type
The PHP object is an instance from the class or from a data structure that has already been created to help us take a copy to serve up a part in the coding.
The PHP object has two parts, attributes and methods. If you need to learn more, you have to read the PHP object tutorial.
Anyway, let’s convert the other data types into a PHP object.
<?php
$obj = (object) "Hello World";
var_dump( $obj );
?>
The result is like the following
object(stdClass)#1 (1) { ["scalar"]=> string(11) "Hello World" }
It is the same job in the casting of other data types into the PHP array. Place the string data into an object as a field.
In the following code, I will cast an integer type to an object. Let’s see how it does work.
<?php
$obj = (object) 155;
var_dump( $obj );
/* The result
===========================
object(stdClass)#1 (1) {
["scalar"]=>
int(155)
}
*/
?>
Also, it replaced the integer value as a field inside the object. Look at the below boolean casting. It will convert it also into an object.
<?php
$obj = (object) true;
var_dump( $obj );
/* The result
===========================
object(stdClass)#1 (1) {
["scalar"]=> bool(true)
}
*/
?>
Also, the same thing with the PHP array
<?php
$obj = (object) array(
"data"
);
print_r( $obj );
/* The result
===========================
stdClass Object
(
[0] => data
)
*/
?>
In the following part, I am going to explain the casting of another data type into PHP float with examples.
Casting Data to PHP Float Type
The PHP float refers to the point in the numbers or numbers that contains a decimal point. In another word, we can name it “double” or “real numbers”.
To convert the other data type into the PHP float, you have to use the same braces as in the below code.
<?php
$float = (float) "CodedTag.com";
var_dump( $float ); // float(0)
?>
To convert an integer to float
<?php
$float = (float) 120;
var_dump( $float ); // float(120)
?>
Boolean to float
<?php
$float = (float) true;
var_dump( $float ); // float(1)
?>
An array to float
<?php
$float = (float) array( "codedtag" );
var_dump( $float ); // float(1)
?>
But if the array is empty, it will show you this result: float(0)
Casting the PHP object into PHP float.
In the following example, it will show you an error
<?php
class Car {
private $model;
}
$object = new Car();
$float = (float) $object;
var_dump( $float );
?>
The output
PHP Warning: Object of class Car could not be converted to float in index.php on line ....
So you will never be able to convert the PHP object to a PHP float.
Wrapping Up
In this article, you learned how to convert the PHP types into other PHP data types. And that was the following list.
- Casting the PHP strings into other data types.
- Casting the Integers into other data types.
- Converting the array to other data types.
- Casting other data types to object
- Also, the other scalar types like float, and boolean into other PHP data types.
During this article, you saw some warnings occurred in the PHP code like PHP warning or PHP error and that was happening because we tried to convert type to another type. And we explained why does that happening.
If you need more details about the PHP types, you have to read the PHP data types tutorial.