我有一个简单的系统,这个计算速率,如何验证数值是否输入?我使用is_numeric但可以使用唯一的变量,什么是替代?
显示此错误:警告:is_nan()预计在第10行的C:\ xampp \ htdocs \ imposto \ processar.php中给出了1个参数,5
按照代码html:
<html lang="pt-br">
<head>
<title>Calc - Imposto</title>
</head>
<body>
<h2>SISTEMA DE CÁLCULO DE IMPOSTOS</h2>
<h3>Subistua vírgula por ponto "."</h3>
<form method="POST" action="processar.php">
<label>Valor Total: </label>
<input type="text" name="valorTotal" style="width: 100px;"></br></br></br>
% <input type="text" name="porcentagem1" style="width: 100px;"></br></br>
% <input type="text" name="porcentagem2" style="width: 100px;"></br></br>
% <input type="text" name="porcentagem3" style="width: 100px;"></br></br>
% <input type="text" name="porcentagem4" style="width: 100px;"></br></br>
<input type="submit" value="Enivar" >
<input type="reset">
</form>
</body>
</html>
按照代码php:
<
?php
$valortotal = $_POST['valorTotal'];
$porcentagem1 = $_POST['porcentagem1'];
$porcentagem2 = $_POST['porcentagem2'];
$porcentagem3 = $_POST['porcentagem3'];
$porcentagem4 = $_POST['porcentagem4'];
if(is_numeric($valortotal, $porcentagem1, $porcentagem2, $porcentagem3, $porcentagem4)){
echo "Por favor, digite apenas números";
}
?>
is_numeric仅检查1个变量。
您必须编写如下语句:
if(is_numeric($valortotal) && is_numeric($porcentagem1) && is_numeric($porcentagem2) && is_numeric($porcentagem3) && is_numeric($porcentagem4))
{
echo "Por favor, digite apenas números";
}
注意:如果所有变量都是数字变量,那么这只是回声的“Por favor,digiteapenasnúmeros”。
首先,你的<?php
有两行。第二个错误告诉你,is_numeric需要你给它一个参数5。
为了检查所有5个变量你可以使用&&
为每个变量,所以5个不同的调用is_numeric()
或你可以做一个数组循环并检查每个变量,如果是数字
<?php
$valortotal = $_POST['valorTotal'];
$porcentagem1 = $_POST['porcentagem1'];
$porcentagem2 = $_POST['porcentagem2'];
$porcentagem3 = $_POST['porcentagem3'];
$porcentagem4 = $_POST['porcentagem4'];
$Verify_Int = array(
$valortotal,
$porcentagem1,
$porcentagem2,
$porcentagem3,
$porcentagem4
);
foreach ($Verify_Int as $element) {
if (is_numeric($element)) {
echo var_export($element, true) . " is numeric", PHP_EOL;
} else {
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
}
}
?>