Search code examples
phplaravelsquare-bracket

laravel php square brackets definition


I'm trying to understand what the square brackets mean when trying to seed a database in Laravel. If I have a table with 2 fields title and body I'm wondering why square brackets are used instead of array(). Are the square brackets used for a short form or for organization? seems that square brackets aren't just used to seed a database.

public function run()
{
    $posts = [
      [ 'title' => 'My first post', 'body' => 'My post' ],
      [ 'title' => 'My second post', 'body' => 'My post' ]
    ];
    DB::table('posts')->insert($posts);
}

Solution

  • This is the same as:

    public function run()
    {
        $posts = array(
          array( 'title' => 'My first post', 'body' => 'My post' ),
          array( 'title' => 'My second post', 'body' => 'My post' )
        );
        DB::table('posts')->insert($posts);
    }
    

    It is the newer (>= PHP 5.4) short way of defining an array.