Arrays in PHP: Some interesting experiments

If you already know programming languages like C,C++ or Java, you will find that the arrays in PHP are somewhat different. In PHP, you don’t have to specify the type of variable. So unlike other programming languages like C/C++ or Java where arrays are a collection of homogenous elements (read as types). In PHP, you can have elements which are of different types. This will be clear when we look at the examples.

We have seen that we don’t have to specify the type of the array. Secondly, we have no need to specify the size of the array. You can just start using array by inserting the elements at appropriate positions

So inserting an element into the array

$varname[position] = value;

Take for example, we insert “Sunday” into the array days.

$days[0] = "Sunday";

Let’s print this value

echo "$days[0]n";

It’s a normal practice followed by almost all the programming languages to start inserting elements from the zeroth position. Though it is mandatory in PHP.

Let’s see the contents of the array days. For this purpose, we use the function var_dump()

var_dump($days);

The output of var_dump will be

array(1) {
  [0]=>
  string(6) "Sunday"
}

From the output, we can understand that days is an array with one element. The element is present at the 0th position with length 6.

In the beginning we said that arrays in PHP can contain elements of different types. Though it is not preferable to use this way of using arrays, let’s try inserting a boolean literal.

$days[0] = "Sunday";
$days[1] = true;

var_dump($days);

The output of var_dump will be

array(2) {
  [0]=>
  string(6) "Sunday"
  [1]=>
  bool(true)
}

It is not necessary to start inserting elements from the 0th position. We try inserting element from the fourth position and see how the array looks like

$months[3] = "April";
var_dump($months);

echo "Second Month: ", $months[1], "n";
echo "Fourth Month: ", $months[3], "n";

The output of var_dump will be

array(1) {
  [3]=>
  string(5) "April"
}
Second Month: 
Fourth Month: April

From the var_dump output, we can see that the array months has only one element and the element is at the position 3. We also tried to print a non existing element, which gave us an empty string.

It is also possible to use negative indices for specifying the position of an element. Let’s try using floating point numbers as indices

$values[2.3] = 10;
var_dump($values);

$values[2.3] = 10;
$values[2.4] = 14;
var_dump($values);

We find that the values after the decimal point (.) is not taken into consideration. And that’s the reason the value is finally updated to 14 and the index value is 2 in both cases (2.3 and 2.4)

array(1) {
  [2]=>
  int(10)
}
array(1) {
  [2]=>
  int(14)
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s