Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Slim Basics & Twig Templates Views & Twig Inside of Slim

Sean Flanagan
Sean Flanagan
33,235 Points

Is there supposed to be a comma on this line?

Hi.

I couldn't help but notice the comma after the TwigExtension parentheses. Since there are no arguments after TwigExtension, should that comma be there?

I'll post the code snippet below:

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(), // I refer to the comma at the end of this line
);

Thanks in advance. :)

1 Answer

Hi Sean,

I think that comma is purely optional. When creating an array, php allows you to optionally put a comma after the last element in the array.

From http://php.net/manual/en/language.types.array.php

The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.

You may want to briefly look over the code examples on that page to see where they've used or didn't use the trailing comma.

So in the case of your code example, the array was split onto multiple lines and so it appears to follow this convention of adding the trailing comma.

Had the array been written on a single line like this:

$view->parserExtensions = array(new \Slim\Views\TwigExtension());

Then the trailing comma wouldn't have been used there if the programmer was following the recommendation in the manual.

$view->parserExtensions = array(new \Slim\Views\TwigExtension(),); // The trailing comma was added here and it's still ok but not following the convention in the manual