简体   繁体   English

为什么我会收到以下代码的偏移错误?

[英]Why do I get an offset error for the code below?

<?php
    $i = 0;

    $array = array('name', 'email', 'address');
    while ($array[$i]) {
        echo "$array[$i]<br>";

        $i++;
    }
?>

PHP Notice: Undefined offset: 3 in /workspace/Main.php on line 6 PHP 注意:未定义的偏移量:3 in /workspace/Main.php on line 6

In while loop until your array has value.在 while 循环中,直到您的数组有价值。 use isset() for it.使用isset() change your while condition as below:更改您的 while 条件如下:

<?php
    $i = 0;

    $array = array('name', 'email', 'address');
    while (isset($array[$i])) {
        echo "$array[$i]<br>";

        $i++;
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM