What’s new in PHP 5.4 – Arrays

Hello all!

Today I’m going to blog about what’s new in PHP.
Recently; a new version of PHP has been released which is version 5.4 which has new features and many improvements rolled out with it.

I’ll blog about array improvements in the latest PHP version in this post; so:

 

What’s new in PHP 5.4? Let’s start with arrays.

I’ll have a simple script here in which I define an array which contains some data and one of the keys pointing to another array:

$myData = array(
	'key'		=>	'value',
	'name'		=>	'foksh',
	'oddNumbers'	=>	array(1,3,5,7,9)
);

Now let's dump this array to see how it looks in the browser:

var_dump($myData);

And here I get my output in the browser:

array(3) {
["key"]=> string(5) "value"
["name"]=> string(5) "foksh"
   ["oddNumbers"]=> array(5) {
      [0]=> int(1)
      [1]=> int(3)
      [2]=> int(5)
      [3]=> int(7)
      [4]=> int(9)
   }
}

What PHP 5 does is that it allows us to replace the keyword array with square brackets [] which works in our array the same way, like the following:

$myData = [
	'key'		=>	'value',
	'name'		=>	'foksh',
	'oddNumbers'	=>	[1,3,5,7,9]

];

var_dump($myData);

So now when I refresh the page I will still get the same output:

array(3) {
["key"]=> string(5) "value"
["name"]=> string(5) "foksh"
   ["oddNumbers"]=> array(5) {
      [0]=> int(1)
      [1]=> int(3)
      [2]=> int(5)
      [3]=> int(7)
      [4]=> int(9)
   }
}

By the way; there is no such thing as short object syntax, so you can't do anything like this:

$obj = {
	'key': 'javascript'
};

Niether this kind of syntax will work:

$obj = {
	'key' => 'javascript'
};

But we can do a little trick of creating an array with an object, like below:

$obj = (object)[
	'key' => 'javascript'
];

var_dump($obj);

So when we dump it, we will have our object as follows:

object(stdClass)#1 (1) {
   ["key"]=> string(10) "javascript"
}

Now lets go back to our array and I will show you something called array dereferencing.
Let's make an example to explain that, we will create a function called getArray, and it will return our array.
Now I will be using short syntax:

function getArray () {
	return [
	'key'		=>	'value',
	'name'		=>	'foksh',
	'oddNumbers'	=>	[1,3,5,7,9]
	];
}

When we want to get oddNumbers data in PHP below version 5.4, we would assign our array to some variable like this:

$mydata = getArray();
var_dump($mydata['oddNumbers'];

So when I refresh the page, I'll get the oddNumbers data:

array(5) {
      [0]=> int(1)
      [1]=> int(3)
      [2]=> int(5)
      [3]=> int(7)
      [4]=> int(9)
}

But in PHP 5.4 you can do something like that:

function getArray () {
	return [
	'key'		=>	'value',
	'name'		=>	'foksh',
	'oddNumbers'	=>	[1,3,5,7,9]
	];
}

var_dump(getArray()['odd_numbers'];

And I still get the same results:

array(5) {
      [0]=> int(1)
      [1]=> int(3)
      [2]=> int(5)
      [3]=> int(7)
      [4]=> int(9)
}

This feature is called array dereference, and it work also for classes, let's create a class with the same function in it which will now become a method:

class Myclass {
	public static function getArray () {
		return [
		'key'		=>	'value',
		'name'		=>	'foksh',
		'oddNumbers'	=>	[1,3,5,7,9]
		];
	}
}

And when I call it I will get the same result:

var_dump(Myclass::getArray()['oddNumbers'];

It works also in objects, so when I create an object and Then replace the static call with normal call, I still get the same result:

class Myclass {
	public function getArray () {
		return [
		'key'		=>	'value',
		'name'		=>	'foksh',
		'oddNumbers'	=>	[1,3,5,7,9]
		];
	}
}

$obj = new Myclass;

var_dump($obj->getArray()['oddNumbers'];

Finally, one more thing I'd like to mention is the register_long_arrays, which in PHP 5.3 was deprecated, and then that directive is totally removed from PHP 5.4, which means you can't run a script that uses this kind of variables in PHP 5.4 ($HTTP_*_VARS).

Example: $HTTP_SESSION_VARS … etc.

That will be all for this post, and I'll blog about other new stuff in PHP 5.4 soon.

Have fun!

Leave a Reply