Introduction to PHP Array


 

Introduction to PHP Array

Introduction to PHP Array, this tutorial covers up the introduction with a simple example on array, the explanation of the program will help you to understand the concept of PHP program in a better way.

Introduction to PHP Array, this tutorial covers up the introduction with a simple example on array, the explanation of the program will help you to understand the concept of PHP program in a better way.

What is Array?

An array in PHP is actually an ordered map, what is a map? A map is a type that associates values to the keys, you can consider as list, stack, hash table, dictionary or an array. An array can be created with the help of array() language construct. It takes number of parameters in the form of key=>value pair separated by comma.

General format of an array is:

array( key=>value)

An example of an array is given below:

<?php

$first=array('Name' =>'Rose', 18=>"true");

echo $first['Name']."<br/>";

echo $first[18];

?>

Output of the above example is:

Rose
true

 In above example

  • <?php ?> is the delimiter, which is mandatory for any PHP program.

  • $first is the name of the variable

  • array() is the language construct

  • 'Name' and 18 are the keys, here character type keys should be enclosed within single or double quote and these are case sensitive, if  you declare a key as 'Name' and if you write 'name' to access that key then an error message will be generated,

  •  echo is the language construct

  • $first['Name'] is the actual way to access the array.

  •  ' . ' operator is used to concatenate  strings.

  • <br/> is used to break line

  • Each line should be ended with a semicolon but at the last line it is optional.

A key may be either an integer or string, floats in the key are truncated to integer.

 

 

 

Ads