PHP Type Casting

Last Updated : May 14, 2021

In this tutorial, we will learn about PHP Type Casting concepts, which support converting variables from one data type into another data type. In PHP, we don’t have to specify the variable’s data type while declaring it; PHP automatically defines the data type based on the value of that variable.

PHP allows us to convert the data type of any variable explicitly using certain built-in keywords. Also, PHP can convert the data types by using the implicit method.

Implicit Type Casting in PHP

PHP will automatically convert the type of a variable based on the value assigned to them. This typecasting is called Implicit, which PHP does for us. For example, if we declare a variable with an empty string and assign an integer value to it later in the program, PHP will convert that variable datatype from a string into an integer automatically.

Explicit Type Casting in PHP

Explicit type casting was done by programmers like us using the keywords allowed by PHP language. Please find below the allowed type casting methods in PHP,

  • (int) or (integer) – cast to an Integer
  • (bool) or (boolean) – cast to a Boolean
  • (float) or (double) or (real) – cast to a Float
  • (string) – cast to a String
  • (array) – cast to an Array
  • (object) – cast to an Object

We will learn about Implicit and Explicit type casting methods in detail below. Also, gettype function used to get the current data type of a variable.

PHP Integer Type Casting

The Integer type casting method allows us to convert any values into integer values. If the value contains floating-point numbers, fractions will be omitted, and strings(without leading numbers) are converted into 0. Please find below the examples,

<?php
$a = (int) '42';
var_dump($a);
$b = (int) 10.5;
var_dump($b);
$c = (int) "5 best programming language";
var_dump($c);
$d = (int) "Best programming language";
var_dump($d);
?>

PHP Boolean Type Casting

The Boolean type casting method allows us to convert any values into the boolean format. Please find below the examples,

<?php
$a = (bool) '';
var_dump($a);
$b = (bool) 10.5;
var_dump($b);
?>

PHP Float Type Casting

The Float type casting method allows us to convert any values to floating-point number. For some type, this will convert the value into integer first then it will convert into float. please find below the examples,

<?php
$number = '10.5';
var_dump($number);
$number = (float) '10.5';
var_dump($number);

$number = (double) '10.5';
var_dump($number);

$number = (real) '10.5';
var_dump($number);
?>

PHP String Type Casting

The String type casting method allows us to convert any values into the string format. A boolean values such as true, false converted into 1, “”(empty string) respectively and null value always converted into “”(empty string). Please find below the examples,

<?php
$a = (bool) true;
var_dump($a);
$b = (string) $a;
var_dump($b);

$c = (string) 42;
var_dump($c);

$d = array(1,2,3,4,5);
var_dump($d);
$e = (string) $d;
var_dump($e);
$f = (bool) null
var_dump($f);
?>

PHP Array Type Casting

The Array type casting method is used to convert a single value from integer, float, string, boolean data types into an array. The first element’s index always will be 0, and the total count of an array is 1. Please find below the examples,

<?php
$a = (array) 42;
echo gettype($a);
var_dump($a);

$b = (array) "Apple"
eco gettype($b);
var_dump($b);
?>

PHP Object Type Casting

The Object type casting method used to convert any values into an object, by default it creates a new instance of the stdClass class. Please find below the example,

<?php
$numbers = array(1,2,3,4,5);
var_dump($numbers);

$object = (object) $numbers
var_dump($object);
?>

We learned about different type casting methods available in PHP. These can be used effectively when we write a program with strict data type conversion. Let’s move to the next chapter of this tutorial series.