Int to Float in PHP

Find this useful? Support us: Star on GitHub 6
Category: Number | Language: PHP

Sure, I'm happy to help!

In PHP, you can convert an integer to a float using the floatval() function. Here's an example:

$myInt = 10;
$myFloat = floatval($myInt);
echo $myFloat; // Output: 10.0

In this example, we first declare a variable $myInt and assign it the value 10. We then use the floatval() function to convert $myInt to a float and assign the result to the variable $myFloat. Finally, we use echo to output the value of $myFloat, which will be 10.0.

You can also use a typecast to convert an integer to a float:

$myInt = 10;
$myFloat = (float) $myInt;
echo $myFloat; // Output: 10.0

In this example, we use the (float) typecast to convert $myInt to a float and assign the result to the variable $myFloat. Again, we use echo to output the value of $myFloat, which will be 10.0.