Look at this PHP code that uses an array. What will it print?
<?php $fruits = ['apple', 'banana', 'cherry']; echo $fruits[1]; ?>
Remember, PHP arrays start counting from 0.
The array has 'apple' at index 0, 'banana' at index 1, and 'cherry' at index 2. So, $fruits[1] is 'banana'.
Why do programmers use arrays in PHP when they want to store many values?
Think about how you keep many items together in one box.
Arrays let you keep many values inside one variable, like a list or box, making it easier to manage data.
Find the error in this PHP code and what error message it will produce.
<?php $colors = array('red', 'green', 'blue'); echo $colors['1']; ?>
PHP automatically converts string numbers to integers when used as array keys.
PHP treats '1' as integer 1 when used as an array index, so it outputs 'green'.
Look at this PHP code using an associative array. What will it print?
<?php $person = ['name' => 'Alice', 'age' => 30]; echo $person['age']; ?>
Associative arrays use keys like 'name' and 'age' to find values.
The key 'age' holds the value 30, so echoing $person['age'] prints 30.
Count the total number of items in this multidimensional array.
<?php $data = [ 'fruits' => ['apple', 'banana'], 'vegetables' => ['carrot', 'lettuce', 'pepper'], 'grains' => ['rice'] ]; ?>
Count all items inside each inner array and add them up.
There are 2 fruits, 3 vegetables, and 1 grain, totaling 6 items.