Automatic Type Conversion in PHP

What is automatic type conversion in PHP?


In PHP, the variable changes from one type to another automatically, based on the value assigned to it. This is possible, as PHP is a loosely typed language. This automatic conversion happens only when the input is valid.

Example :

$var3=300 ."Hscripts";
echo $var1."<br />";
echo $var2."<br />";
echo $var3."<br />"; ?>

Result :


In the above example,
$var1="200"+17; converts the string "200" into an integer value, as it is given in a mathematical expression.
$var2="200"+17.1; converts the string "200" into float since, it is added with a float value.
$var3=300 ."Hscripts"; here the integer value 300 is converted to a string value automatically.

PHP Topics

Ask Questions

Ask Question