-2
<?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

Blaze
  • 59
  • 7
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – BenRoob Sep 27 '17 at 09:19
  • Works perfectly for me... – Twinfriends Sep 27 '17 at 09:20
  • var_dump($array[$i]) in while should point you the problem. – BenRoob Sep 27 '17 at 09:20
  • 1
    Btw it is a notice and not an error. To fix it you need to check if the value is set while(isset($array[$i])) – Mr Pro Pop Sep 27 '17 at 09:21

1 Answers1

4

In while loop until your array has value. use isset() for it. change your while condition as below:

<?php
    $i = 0;

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

        $i++;
    }
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47