The following code shows Examples of Typecasting of Variables in PHP.

<?php
echo '<h1>Example Demonstrating Type Casting of Variables in PHP</h1>';

$a=100;
$b=10.119;
$c=true;
$d='A String!';
echo 'a='.$a.' (Integer Variable)<br/>b='.$b.' (Float Variable)<br/>c='.$c.' (Boolean Variable)<br/>d='.$d.' (String Variable)';

$x1=(int)$b;
$x2=(float)$a;
$x3=(bool)$a;
$x4=(float)$c;
$x5=(unset)$d;
echo '<h2>After Typecasting...</h2>';
echo '<br/>'.$x1.' (Float Typecasted to Int)<br/>';
echo  $x2.' (Int Typecasted to Float)<br/>';
echo  $x3.' (Int Typecasted to Bool)<br/>';
echo  $x4.' (Bool Typecasted to Float)<br/>';
echo  '$x5='.$x5.'<br/>';
echo '(String Typecasted to null)<br/>';


?>

Output

Typecasting of Variables in PHP
Typecasting of Variables in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

PHP Practice Questions