Why do PHP Array Examples Leave a Trailing Comma?

后端 未结 10 1581

I have seen examples like the following:

$data = array(
   \'username\' => $user->getUsername(),
   \'userpass\' => $user->getPassword(),
   \'em         


        
10条回答
  •  -上瘾入骨i
    2020-12-05 10:02

    Because it keeps entries uniform.

    If you've had to swap the order, or add or delete entries, you know being able to leave a trailing comma is very convenient.

    If the last element cannot have a comma, then you end up having to maintain the last comma by modifying entries. It's a pointless exercise and a waste of time and finger strokes because the intent of swapping or modifying entries is already accomplished.

    By allowing a trailing comma on the last element, it frees the programmer from having to tend to this annoying and fruitless detail.

提交回复
热议问题