Casting or type casting is a process to convert a data type into another data type. PHP allows to convert data types into other data types and following functions are available.

(string)Converts to data type String
(int)Converts to data type Integer
(float)Converts to data type Float
(bool)Converts to data type Boolean
(array)Converts to data type Array
(object)Converts to data type Object
(unset)Converts to data type NULL

To cast to any data type, use (datatype) in parenthesis before variable or value.

Syntax

variable_name = (datatype) variable_name | value;

Convert or cast to string

<?php
$a = 25;       // Integer
//check data type before casting to string
var_dump($a);

$a = (string) $a;

//check data type after casting
var_dump($a);
?>

Convert or cast to int

<?php
$a = "15";       // string
//check data type before casting to Integer
var_dump($a);

$a = (int) $a;

//check data type after casting
var_dump($a);
?>

Convert or cast to float

<?php
$a = 30;       // Integer
//check data type before casting to Integer
var_dump($a);

$a = (float) $a;

//check data type after casting
var_dump($a);
?>

Convert or cast to bool

<?php
$a = 30;       // Integer
//check data type before casting to bool
var_dump($a);

$a = (bool) $a;

//check data type after casting
var_dump($a);
?>

Convert or cast to Array

<?php
$a = 130;       // Integer
//check data type before casting to array
var_dump($a);

$a = (array) $a;

//check data type after casting
var_dump($a);
?>

Convert or cast to object

<?php
$a = 130;       // Integer
//check data type before casting to object
var_dump($a);

$a = (object) $a;

//check data type after casting
var_dump($a);
?>

Convert or cast to null

<?php
$a = 130;       // Integer
//check data type before casting to null
var_dump($a);

$a = (unset) $a;

//check data type after casting
var_dump($a);
?>