ClassesPHP classesPHP Variables

What is an array in PHP?

In PHP, an array is a data structure that can store multiple values under a single variable name. Each value in an array is accessed using a unique index or key. PHP supports both indexed arrays (where keys are integers starting from 0) and associative arrays (where keys can be any valid PHP data type, such as strings or integers). Arrays in PHP can hold a mixture of different data types, including strings, integers, floats, objects, or even other arrays.

Here’s a basic example of an indexed array in PHP:

$colors = array("Red", "Green", "Blue");

And here’s an example of an associative array:

$person = array("name" => "John", "age" => 30, "city" => "New York");

You can access array elements using their index or key:

echo $colors[0]; // Outputs: Red

echo $person["name"]; // Outputs: John

Arrays in PHP are very flexible and powerful, offering a wide range of functions and methods for manipulation and iteration.

Leave a Reply

Your email address will not be published. Required fields are marked *