Jump to content

Array Name: Singular or Plural?


doubledee

Recommended Posts

Should a PHP Array name be Singular or Plural?

 

If I follow what you do for Tables in a database, then they would be Singular.

 

I saw something online where someone suggested appending "List" to the end of the Array Name so you have something like...

$nameList
$answerList

 

Or maybe you could do something *obvious* like append "Array" on the end like this...

$nameArray
$answerArray

 

What do you think??

 

 

Debbie

 

 

Link to comment
Share on other sites

I got stuck into the habit of naming variables based on what they are, I blame an old tutor for that.

 

I name things like this.

 

<?php
// Strings..
$strFname = "Bruce";

// Integers
$intMultiplier = 3;

// Arrays
$arrItems = array("itemone", "itemtwo", "itemthree");

 

I've grown on that now where if I want to name a variable by something that to me is descriptive (usually one or two words) if it's two words I condense the first word to the capital form of it's first letter.. So first name to me is $strFname, which means $strLname is last name.

 

It really is a personal preference thing, and as you develop/code more you'll almost automitcally develop your own naming style/convention.

Link to comment
Share on other sites

I got stuck into the habit of naming variables based on what they are, I blame an old tutor for that.

 

Microsoft teaches its developers to code that way.

 

Not really.  At least, not in any of the MS related material I've encountered (which is sizeable, since I've been writing more C# than PHP lately).

 

That style, FWIW, is called Hungarian Notation.  It's not very popular.

 

As far as array names go, I'd strongly advise NOT ending them with 'List'.  A list is an actual data structure in other languages, and as such, implies certain things.

 

I tend to pluralize my array names, as they contain multiple elements of related data.

Link to comment
Share on other sites

I tend to pluralize my array names, as they contain multiple elements of related data.

 

I agree. If you encounter a variable named $answer you're likely to assume it contains a single answer, where as $answers obviously implies there's multiple. Plus say in a foreach loop it means you can keep the naming semantic:

 

foreach ($answers as $answer) {

 

.. Without having to create odd or meaningless names like:

 

foreach ($answer as $answerItem) {
foreach ($answer as $item) {

Link to comment
Share on other sites

Sometimes I pluralize, sometimes I don't. It depends on the context. For example, if we are talking about an associative array for a single user I will make the name singular; probably $user. If it is a multidimensional array containing many users, I will likely pluralize it, so it would be $users.

 

Honestly, it really makes no difference. It's just personal preference.

Link to comment
Share on other sites

I was asking about these 3 choices...

$answer();

$answers();

$answerList();

 

There is no real right or wrong, but out of the three the first is the worst. The second two are at least clear that there are multiple items. The third is most descriptive depending on what other variables you might have. I think $arrAnswers or $answersArr is kind of annoying honestly but I might be the only one.

 

Actually, all of them are wrong, because you've made them a function using a variable string. Functions end in (), variables start with $. While it's possible to do $answer();, what you're doing is calling a function with the name stored in $answer. So it could be

$answer = 'bob'; 
$answer(); 

and it would be the same as bob();

Link to comment
Share on other sites

Sometimes I pluralize, sometimes I don't. It depends on the context. For example, if we are talking about an associative array for a single user I will make the name singular; probably $user. If it is a multidimensional array containing many users, I will likely pluralize it, so it would be $users.

 

Honestly, it really makes no difference. It's just personal preference.

 

This is pretty much what I do, as well.

 

I think $arrAnswers or $answersArr is kind of annoying honestly but I might be the only one.

 

At face value I can see why people would do that, especially in a loosely typed language like php.  But yeah...nonetheless, I too find it annoying.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.