Core PHP

Convert a String to a Number

This example PHP program demonstrates how to convert a string to a number.

ConvertStringToNumber.php

<?php

$sValue = "3.14159";
echo "Original Value = ".$sValue."<br /><br />";
// Conversion Functions
echo "IntVal() = ".IntVal($sValue)."<br />";
echo "FloatVal() = ".FloatVal($sValue)."<br /><br />";
// Type Casting
// These are both integer type
echo "(int) = ".(int)$sValue."<br />";
echo "(integer) = ".(integer)$sValue."<br />";
// These are all float type
echo "(float) = ".(float)$sValue."<br />";
echo "(double) = ".(double)$sValue."<br />";
echo "(real) = ".(real)$sValue."<br /><br />";
// SetType()
$sSetValue = $sValue;
SetType($sSetValue, "int");
echo "SetType($sSetValue, \"int\") = ".$sSetValue."<br />";
$sSetValue = $sValue;
SetType($sSetValue, "integer");
echo "SetType($sSetValue, \"integer\") = ".$sSetValue."<br />";
$sSetValue = $sValue;
SetType($sSetValue, "float");
echo "SetType($sValue, \"float\") = ".$sSetValue."<br />";
$sSetValue = $sValue;
SetType($sSetValue, "double");
echo "SetType($sValue, \"double\") = ".$sSetValue."<br />";
?>
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.