We can construct ordered arrays with a built-in PHP function: array().

The array() function returns an array. Each of the arguments with which the function was invoked becomes an element in the array (in the order they were passed in).

Arrays are most useful when we store them in variables. We create an array variable the same way we create variables of other data types—with the assignment operator.

$my_array = array(0, 1, 2);

In the code above, we constructed an array using the array() function which we captured with the $my_array variable. $my_array is an array with three elements: 0 is located in the 0th index, 1 in the 1st, and 2 in the 2nd.

PHP arrays can store elements of any data type:

$string_array = array("first element", "second element");

In the code above, $string_array holds two string elements. The string "first element" is located at the 0th location index, and the string "second element" is located at the 1st.

PHP arrays can also store elements of multiple data types:

$mixed_array = array(1, "chicken", 78.2, "bubbles are crazy!");

Above, $mixed_array holds four elements—some are strings while others are numbers.

We can use the built-in PHP count() function to get the number of elements in an array. This is especially useful as we work with larger and more complicated arrays:

echo count($my_array); // Prints: 3 echo count($string_array); // Prints: 2 echo count($mixed_array); // Prints: 4

Let’s create some arrays!



Create an array variable $first_array. It should have 5 elements. The elements in the 0th, 2nd, and 4th index locations should be strings, and the elements in the 1st and 3rd index locations should be numbers.


Use echo and the built-in count() function to print the length of your array.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?