j.silver Posted November 18, 2015 Share Posted November 18, 2015 Hi, This block code is from W3schools tutorials: <?php $cars = array("Volvo", "BMW", "Toyota"); $arrlength = count($cars); for($x = 0; $x <= $arrlength; $x++) { echo $cars[$x]; echo "<br>"; } ?> The original block was without the equal sign in $x <= $arrlength; I have added the equal sign to see what would happen. I got the same result but followed with: Notice: Undefined offset: 3 in C:\xampp\htdocs\Testing\testing.php on line 14 I would appreciate it if someone would explain why I am getting this notice and its meaning. I thought the loop will stop and give the same result without any notice when it hits no value for the equal sign. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 18, 2015 Share Posted November 18, 2015 The length of the array is 3, and you are telling it to loop as long as $x is less than or equal to 3. When $x is 3 it tries to access $cars[3], which does not exist. Link to comment Share on other sites More sharing options...
Chikwado Posted November 21, 2015 Share Posted November 21, 2015 You need to understand what this "<=" means in php. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 21, 2015 Share Posted November 21, 2015 An array index value always begin with 0, so index will be 0, 1, 2, the array length will produce 3, using equals will index value 3 as the loop finishes, which as you can see does not exist. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now