PHP Arrays

0
3K

An array stores multiple values in one single variable:

ExampleGet your own PHP Server

$cars = array("Volvo", "BMW", "Toyota");
Try it Yourself »

What is an Array?

An array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or name.


PHP Array Types

In PHP, there are three types of arrays:


Working With Arrays

In this tutorial you will learn how to work with arrays, including:


Array Items

Array items can be of any data type.

The most common are strings and numbers (int, float), but array items can also be objects, functions or even arrays.

You can have different data types in the same array.

Example

Array items of four different data types:

$myArr = array("Volvo", 15, ["apples", "bananas"], myFunction);
Try it Yourself »

Array Functions

The real strength of PHP arrays are the built-in array functions, like the count() function for counting array items:

Example

How many items are in the $cars array:

$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
Try it Yourself »

For a complete reference of all array functions, go to our complete PHP Array Reference.


PHP Exercises

Test Yourself With Exercises

Exercise:

Use the correct function to output the number of items in an array.

$fruits = array("Apple", "Banana", "Orange");
echo ;

Start the Exercise

Search
Categories
Read More
Other
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
By PHP Tutorial 2024-05-17 07:45:23 0 2K
Other
PHP Introduction
PHP code is executed on the server. What You Should Already Know Before you continue you...
By PHP Tutorial 2024-05-17 07:08:40 0 3K
Other
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
By PHP Tutorial 2024-05-17 07:09:40 0 3K
Other
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to the browser....
By PHP Tutorial 2024-05-17 07:10:53 0 4K
Other
PHP Array Functions
PHP Array Functions PHP has a set of built-in functions that you can use on arrays....
By PHP Tutorial 2024-05-17 08:07:42 1 5K