代码之家  ›  专栏  ›  技术社区  ›  ecoboff1

循环中未定义的偏移通知

  •  1
  • ecoboff1  · 技术社区  · 12 年前

    我正在使用一个脚本来创建额外的表单行。 它循环某些文本字段的$i数字 当我运行这个脚本时,偏移量是未定义的,因为它不存在。 我需要使用if(isset())函数,但我不确定如何将其放入代码中。 有人能帮忙吗?

    for ($i=0; $i<100; $i++) {
    
        if ($text['length'][$i] == "") $text['length'][$i] = "0";
        if ($text['bredth'][$i] == "") $text['bredth'][$i] = "0";
        if ($text['height'][$i] == "") $text['height'][$i] = "0";
        if ($text['weight'][$i] == "") $text['weight'][$i] = "0.00";
    

    以“if”开头的所有行显示通知:

    注意:未定义的偏移量:第41行C:\examplep\htdocs\newparcelscript.php中的1

    解决了的 事实i根本不需要和“if”雄蕊,因为行的创建和值的设置是一起运行的。

    for ($i=0; $i<100; $i++) {
    
       $text['length'][$i] = "0";
       $text['breadth'][$i] = "0";
       $text['height'][$i] = "0";
       $text['weight'][$i] = "0.00";
    
    3 回复  |  直到 12 年前
        1
  •  0
  •   Jordan Doyle    12 年前

    我认为测试 empty() 这就是你在这里寻找的。

    for($i = 0; $i < 100; $i++)
    {
        if(empty($text['length'][$i]) === TRUE) $text['length'][$i] = 0;
        ...
        ...
    }
    
        2
  •  0
  •   Scuzzy    12 年前

    根据你想在这里做什么,我建议你使用以下isset/空的组合之一

    if (isset($text['length'][$i]) == false or empty($text['length'][$i]) == true)
    
    if (isset($text['length'][$i]) == true and empty($text['length'][$i]) == true)
    

    错误很可能来自对不存在的索引的测试: if($text['length'][$i]==“”)

        3
  •  0
  •   Community CDub    7 年前

    对于这种情况,如果需要为未定义的字段插入值,请使用 empty() .

    空() 如果值为空字符串,则返回TRUE,而 !isset() 将返回FALSE。
    例如,这方面有很多问题 look here .

    类似这样的内容:

    for ($i=0; $i<100; $i++) {
        if (empty($text['length'][$i])) $text['length'][$i] = "0";
        if (empty($text['bredth'][$i])) $text['bredth'][$i] = "0";
        if (empty($text['height'][$i])) $text['height'][$i] = "0";
        if (empty($text['weight'][$i])) $text['weight'][$i] = "0.00";
    }