【问题标题】:how to stop a non-numeric value encountered warning in php如何停止在php中遇到警告的非数字值
【发布时间】:2021-02-20 00:29:40
【问题描述】:

我试图更好地理解 php,所以我制作了一个转换不同单位的应用程序。在我的代码中,我要求用户输入长度、体积和重量单位的值,然后选中多个复选框以转换他们想要的单位。除了在输入框中不输入任何内容外,我一切正常,我收到警告 A non-numeric value 在“”中遇到。所以我的问题是如何停止显示此警告并让它说您忘记输入输入。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="convert_units2.php" method="POST"> 
    <?php
        //use from other php code
        // require("form_functions.php");
        //
        // include("form_functions.php");
        function generate_options_pulldown($options){
            echo '<select name = "' . $options["title"] . '_unit">';
            foreach($options["list"] as $opt){
                echo '<option value="' . $opt . '">' . $opt . '</option>';
            }
            echo '</select>';
        }
        function generate_options_checkbox($options){
            foreach($options["list"] as $opt){
                echo '<lable><input type ="checkbox" name="'. $opt . '">' .$opt .'</label>';
            }
        }
        function generate_conversion_form($units){
            echo '<input type="text" size="10" name="' . $units["title"] . '_val"> ';
            generate_options_pulldown($units);
            echo "-->";
            generate_options_checkbox($units);
        }
        //
        $length_units = array("title"=>"length","list"=>array("meter","mm","cm","km"));
        $volume_units = array("title"=>"volume","list"=>array("liter","oz","gallon"));
        $weight_units = array("title"=>"weight","list"=>array("gram","mg","kg"));

        if ($_SERVER["REQUEST_METHOD"] == "GET"){
            generate_conversion_form($length_units);
            echo "<p>";
            generate_conversion_form($volume_units);
            echo "<p>";
            generate_conversion_form($weight_units);
            echo '<p><input type="submit" value="Convert"></p>';
        }
        else{//Post
            // var_dump($_POST);
            $length_units_conversion_table = array("meter"=>1,"mm"=>1000,"cm"=>100,"km"=>0.001);
            $from_unit = $_POST["length_unit"];
            $val = $_POST["length_val"];
            $meter = $val / $length_units_conversion_table[$from_unit];
            foreach($length_units_conversion_table as $unit => $rate){
                if(isset($_POST[$unit]) and $_POST[$unit] == 'on' )
                printf("%s %s is %s %s<br>",$val, $from_unit,$meter * $rate, $unit);   
            }
            
            echo '<br>';
            $volumn_units_conversion_table = array("liter"=>1,"oz"=>33.8,"gallon"=>0.264);
            $from_unit = $_POST["volume_unit"];
            $val = $_POST["volume_val"];
            $liter = $val / $volumn_units_conversion_table[$from_unit];
            foreach($volumn_units_conversion_table as $unit => $rate){
                if(isset($_POST[$unit]) and $_POST[$unit] == 'on' )
                printf("%s %s is %s %s<br>",$val, $from_unit,$liter * $rate, $unit);
            }
            echo '<br>';
            $weight_units_conversion_table = array("gram"=>1,"mg"=>1000,"kg"=>0.001);
            $from_unit = $_POST["weight_unit"];
            $val = $_POST["weight_val"];
            $gram = $val / $weight_units_conversion_table[$from_unit];
            foreach($weight_units_conversion_table as $unit => $rate){
                if(isset($_POST[$unit]) and $_POST[$unit] == 'on' )
                printf("%s %s is %s %s<br>",$val, $from_unit,$gram * $rate, $unit);
            }
        }
    ?>
    
    </form>
</body>
</html>

【问题讨论】:

  • 它会给你一个带有错误信息的行号吗?
  • 另外,请分享您的调试尝试

标签: php


【解决方案1】:

您可以使用函数 is_numeric() (https://www.php.net/manual/en/function.is-numeric.php) 检查值是否为数字

if(!is_numeric($_POST["volume_val"])) { ...} ) 

对于没有设置值的特定情况,您可以检查该值是否不是空字符串,

if( $_POST["volume_val"] == '') {...} 

或将空字符串转换为零

$volume_val = empty($_POST["volume_val"]) ? 0 : $_POST["volume_val"];

【讨论】:

    【解决方案2】:

    对于空字段提醒,您可以在输入中使用 required 属性,因此当您提交表单时,您会收到“必填字段”警告并且表单不会提交。

    然后,在代码的 POST 部分,您可以测试 $_POST["length_unit"] 是否为空: if (empty(trim($_POST["length_unit"])))

    如果这是真的,那么...您可以将其视为 0 或仅发送警告。

    您还可以在处理之前测试数值: if (is_numeric($_POST["length_unit"]))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      相关资源
      最近更新 更多