PHP Arrays

0
6χλμ.

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

Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
άλλο
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
από PHP Tutorial 2024-05-17 07:47:03 0 4χλμ.
άλλο
PHP - Escape Characters
Escape Character To insert characters that are illegal in a string, use an escape character. An...
από PHP Tutorial 2024-05-17 07:32:36 0 11χλμ.
άλλο
PHP switch Statement
The switch statement is used to perform different actions based on different...
από PHP Tutorial 2024-05-17 07:47:56 0 4χλμ.
άλλο
الثورة البلوكتشين وتحولاتها في الصناعات المختلفة
مقدمة: شهدت تقنية البلوكتشين ثورة هائلة في السنوات الأخيرة، حيث أصبحت لديها القدرة على تغيير...
από MOHAMED ATTALLAH 2024-05-14 14:03:03 0 7χλμ.
άλλο
PHP Delete Array Items
Remove Array Item To remove an existing item from an array, you can use...
από PHP Tutorial 2024-05-17 08:06:22 0 7χλμ.
Sociallez https://sociallez.com