Loading...

TapGen

Free online tutorials

img

PHP arrays are a useful way to store multiple elements of the same type in one variable. using php array functions making it easy to create lists of similar elements that can be accessed by index or key.

For example: if you want to store the 3 fruits name in a loop, then instead of defining 3 variables. you can do that easily and shortly with PHP array argument.

PHP array functions with examples

To create an Indexed array you need to use two function.

  • array(): Creates an array.
  • range(): Creates an array containing a range of elements.

Lets see the example:

 
$fruits = array("apple", "banana", "orange");
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";


Output:

img

Indexed arrays function in php

Indexed arrays in PHP are simple arrays that use numeric indexes to store values. that means, each item has an index number in the array.

By default, PHP Indexed Arrays are Sarting from number 0. Then 1,2,3 and more. Each item in an indexed array can be accessed using its index number.

See the example again, $fruits[0] is targeting apple from array

Using shorthand syntax

you can use shorthand syntax (the square bracket [] notation) to create and manipulate indexed arrays.

 
 // Creating an indexed array 
$fruits = ["apple", "banana", "orange"]; 
// Accessing items 
echo $fruits[0]; // Outputs: apple 
 
 

Indexed arrays are a fundamental data structure in PHP that allow you to store and manipulate collections of data using numeric indexes. 

 

If you have any questions or need further examples, let us know and upload the question in our forum tapgen.xyz

Learn to code with the world's largest web developer site. Not sure where to begin? HTML is The language for building web pages Learn HTML Tutorial with Tapgen.

=================================
Share This Article
icon

New to Communities?

Join the community