Automatic Type Conversion in PHP
What is automatic type conversion in PHP?
Explanation
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 :
<?php
$var1="200"+17;
$var2="200"+17.1;
$var3=300 ."Hscripts";
echo $var1."<br />";
echo $var2."<br />";
echo $var3."<br />"; ?>
Result :
217
217.1
300Hscripts
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.