PHP Arrays

tutorial and example
tutorial and example

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable.

Following are the advantages of Array.

  • It reduces the code.
  • We can store data easily.
  • Easy to traverse.

How to create Array in PHP

In PHP, the array() function is used to create an array.

Syntax:

Example

Output

10

There are three types of array in PHP:

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

PHP Indexed Arrays

PHP indexed array is used to store the number, string and object. Itis represented by number andstarts with 0.

There are two ways to represent indexed array:

1stway:

$name=array(“Rohit”,”Rohan”,”Shyam”);

Example 1:

Output

Your Name: Geeta,Rohan,Shyam

2nd Ways:

Example2

Output

Your Name: Geeta,Rohit,Shyam

Exqmple3

Output

Hello John
Hello Ram
Hello Rohan

Example4

Output

3