Quantcast
Viewing all articles
Browse latest Browse all 5

Arrays in PHP

Simply an Array is a group of the data items or we can say An Array is a set of variables that all have same name. for example we can define an array Capitals to represent a set of the capitals name of the some countries. See the following example:

  1. $Capitals[1] = "New Delhi";
  2. $Capitals[2] = "Washington";

Assign values to an array in any order

We can assign values to array in any order, see the following lines of code:

  1. $Capitals[5] = "Tokyo";
  2. $Capitals[3] = "Sydney";

Use Characters as Index to assign values

From the above we can see that we are using numeric value to assign values to the array Capitals. We can use characters instead. The characters are inside quotes in the brackets, see the following example:

  1. $Capitals[“A”] = "Londan";
  2. $Capitals[“B”] = "Paris";

And we can get these element from the array as :

  1. Echo $capitals["A"];

An array in PHP hold different data types and variables.

An array in PHP hold different data types and variables. See the following example:

  1. $Num[1]=5;
  2. $Num[2]= "One";
  3. $Num[3]=$var;

Initialization of Arrays

In PHP we can create an array without indexing. see this:

  1. $Capitals[] = "New Delhi";
  2. $Capitals[] = "Washington";

PHP do it automatically and another another way to initialize an array is to use explicit index values . see the following example:

  1. $Capitals[0] = "New Delhi";
  2. $Capitals[1] = "Washington";

One thing that you can see in the all above code, we can create an array without specify its size.
And Now another way to initialize an array: we can initialize an array by array construct, see the following code:
$Capitals= array( “New Delhi”,”Washington”);


Viewing all articles
Browse latest Browse all 5

Trending Articles