How can we convert a PHP string to a number?

In PHP, the intval() function can be used to convert a string to an integer. Here is an example:

$str = "1234";
$num = intval($str);
echo $num;  // 输出: 1234

To convert a string to a float, you can use the floatval() function or type cast it as (float). Here is an example:

$str = "3.14";
$num = floatval($str);
echo $num;  // 输出: 3.14

// 或者使用强制转换
$num = (float)$str;
echo $num;  // 输出: 3.14

It should be noted that if a string cannot be converted to a number, it will return 0.

bannerAds