PHP Arrays

0
6K

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

Cerca
Categorie
Leggi tutto
Altre informazioni
PHP Operators
PHP Operators Operators are used to perform operations on variables and values. PHP divides the...
By PHP Tutorial 2024-05-17 07:41:39 0 9K
Altre informazioni
PHP if...else Statements
PHP - The if...else Statement The if...else statement executes some code if a...
By PHP Tutorial 2024-05-17 07:46:12 0 4K
Altre informazioni
PHP Magic Constants
PHP Predefined Constants PHP has nine predefined constants that change value depending on where...
By PHP Tutorial 2024-05-17 07:40:16 0 5K
Altre informazioni
PHP - $_SERVER
$_SERVER $_SERVER is a PHP super global variable which holds information about headers,...
By PHP Tutorial 2024-05-17 08:08:50 0 7K
Altre informazioni
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings. PHP Numbers...
By PHP Tutorial 2024-05-17 07:37:05 0 6K
Sociallez https://sociallez.com