Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Last active December 11, 2015 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennuttall/4652096 to your computer and use it in GitHub Desktop.
Save bennuttall/4652096 to your computer and use it in GitHub Desktop.
Code snippets from PHP 5.4 blog post
<?php
// PHP 5.3
$arr = array();
$arr2 = array('a','b','c');
$arr3 = array('a' => 2, 'b' => 4, 'c' => 8);
$arr4 = array(array('abc','def'), array(2,4,8), 16);
// PHP 5.4
$arr = [];
$arr2 = ['a','b','c'];
$arr3 = ['a' => 2, 'b' => 4, 'c' => 8];
$arr4 = [['abc','def'], [2,4,8], 16]];
Notice: Array to string conversion in [...] on line 8
<?php
function makeArray() {
return [1,2,3];
}
// The PHP 5.3 way
$arr = makeArray();
echo $arr[2];
// The PHP 5.4 way
echo makeArray()[2]; // returns 3
<?php
// PHP 5.3
$f = new Foo();
echo $f->bar();
// PHP 5.4
echo (new Foo)->bar();
<?php
trait FooBar {
...
}
class Foo {
use FooBar;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment