我想将这些类型的值“3”、“2.34”、“0.234343”等转换为数字。在JavaScript中,我们可以使用Number(),但在PHP中有没有类似的方法?
Input Output
'2' 2
'2.34' 2.34
'0.3454545' 0.3454545
我想将这些类型的值“3”、“2.34”、“0.234343”等转换为数字。在JavaScript中,我们可以使用Number(),但在PHP中有没有类似的方法?
Input Output
'2' 2
'2.34' 2.34
'0.3454545' 0.3454545
当前回答
现在我们正处于一个严格/强类型在PHP中具有更大重要性的时代,我使用json_decode:
$num = json_decode('123');
var_dump($num); // outputs int(123)
$num = json_decode('123.45');
var_dump($num); // outputs float(123.45)
其他回答
如果你事先不知道你有一个浮点数还是一个整数,如果字符串可能包含特殊字符(如空格、欧元等),并且如果它可以包含多于1个点或逗号,您可以使用此功能:
// This function strip spaces and other characters from a string and return a number.
// It works for integer and float.
// It expect decimal delimiter to be either a '.' or ','
// Note: everything after an eventual 2nd decimal delimiter will be removed.
function stringToNumber($string) {
// return 0 if the string contains no number at all or is not a string:
if (!is_string($string) || !preg_match('/\d/', $string)) {
return 0;
}
// Replace all ',' with '.':
$workingString = str_replace(',', '.', $string);
// Keep only number and '.':
$workingString = preg_replace("/[^0-9.]+/", "", $workingString);
// Split the integer part and the decimal part,
// (and eventually a third part if there are more
// than 1 decimal delimiter in the string):
$explodedString = explode('.', $workingString, 3);
if ($explodedString[0] === '') {
// No number was present before the first decimal delimiter,
// so we assume it was meant to be a 0:
$explodedString[0] = '0';
}
if (sizeof($explodedString) === 1) {
// No decimal delimiter was present in the string,
// create a string representing an integer:
$workingString = $explodedString[0];
} else {
// A decimal delimiter was present,
// create a string representing a float:
$workingString = $explodedString[0] . '.' . $explodedString[1];
}
// Create a number from this now non-ambiguous string:
$number = $workingString * 1;
return $number;
}
$a = "10";
$b = (int)$a;
您可以使用它在PHP中将字符串转换为int。
我一直在阅读答案,没有看到任何人提到PHP数字转换中最大的警告。
最受欢迎的答案是:
$str = "3.14"
$intstr = (int)$str // now it's a number equal to 3
太棒了。PHP进行直接铸造。但如果我们做了以下操作呢?
$str = "3.14is_trash"
$intstr = (int)$str
PHP认为这种转换有效吗?
显然是的。
PHP读取字符串,直到找到所需类型的第一个非数字字符。这意味着对于整数,数字字符是[0-9]。因此,它读取3,因为它在[0-9]字符范围内,所以它继续读取。读取。并在那里停止,因为它不在[0-9]范围内。
同样的情况也会发生,如果你被投到浮动或双倍。PHP将读取3。,然后是1,然后是4,并将在i处停止,因为它不是有效的浮点数字字符。
因此,“million”>=1000000计算结果为假,但“10000000000”>=10000计算结果为真。
另请参见:
https://www.php.net/manual/en/language.operators.comparison.php比较时如何进行转换
https://www.php.net/manual/en/language.types.string.php#language.types.string.conversion字符串如何转换为各自的数字
使用一元运算符(+)。例如:
$n1 = +'7';
$n2 = '2.34';
$n2 = +$n1;
var_dump($n1):int(7)var_dump($n2):浮点(2.34)
要避免出现问题,请尝试intval($var)。一些示例:
<?php
echo intval(42); // 42
echo intval(4.2); // 4
echo intval('42'); // 42
echo intval('+42'); // 42
echo intval('-42'); // -42
echo intval(042); // 34 (octal as starts with zero)
echo intval('042'); // 42
echo intval(1e10); // 1410065408
echo intval('1e10'); // 1
echo intval(0x1A); // 26 (hex as starts with 0x)
echo intval(42000000); // 42000000
echo intval(420000000000000000000); // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8); // 42
echo intval('42', 8); // 34
echo intval(array()); // 0
echo intval(array('foo', 'bar')); // 1
?>