Site icon Tutorial

Reading array values

Reading array values

In PHP, you can read values from an array using the array key. The syntax for accessing an array value is as follows: $array_name[‘key’]. Here, $array_name refers to the name of the array, and ‘key’ is the index of the value you want to access.

For example, consider the following array:

$colors = array(‘red’, ‘green’, ‘blue’);

To access the value at index 1 (which is ‘green’), you would use the following code:

echo $colors[1];

This would output “green” to the screen.

You can also use a loop to iterate over all the elements in an array and read their values one by one. For instance, you can use a foreach loop as follows:

foreach ($colors as $color) { echo $color . ‘<br>’; }

This would output each color in the array on a separate line.

Apply for PHP Certification!

https://www.vskills.in/certification/certified-php-developer

Back to Tutorials

Exit mobile version