SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,923
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Array Name: Singular or Plural?

    When it comes to Arrays in PHP, which style do you use...

    1.) Singular
    Code:
    $answer();
    $name();

    2.) Plural
    Code:
    $answers();
    $names();

    3.) Append "List"
    Code:
    $answerList();
    $nameList();

    4.) Append "Array"
    Code:
    $answerArray();
    $nameArray();

    I sorta like #3 or #4...


    Debbie

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Semantically, appending list or plural makes most sense.

    However I personally don't use any plural because I hate having to double check spellings and trying to remember if i made a certain variable/reference plural or singular. Then again I'm not a purist so it's acceptable to me, to someone who is, they might expect it to be semantically correct.

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,849
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    When it comes to Arrays in PHP, which style do you use...

    4.) Append "Array"
    Code:
    $answerArray();
    $nameArray();
    That's how I do it. It's unambiguous. You and anyone else who reads the code knows right away that it is an array.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would write it like $aAnswers, where the first a stands for array
    My colleagues do the same thing. For us its clear. But don't know if other people will read the a as array.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    I probably use "2.) Plural" most often. So long as the names are clearly describing the contents, it's fine whatever you pick. I've never been a big fan of putting the type of the value into the variable name (whether $peopleArray, or hungarian notation).
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Hi,

    I use plurals

    $names = array();

    $colours = array('white' => '#FFFFFF', 'black' => '#000000', 'light-gray' => '#dddddd');

    $Regards = ['first_name' => 'Steve', 'last_name' = 'Browning'];
    ictus==""

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    I use plurals for arrays because there is more than one and makes the following line more readable.

    PHP Code:

      
    foreach( $items as $id => $item ):

         
    $echo $id ' ==> ' $item '<br />';

      endforeach; 
    Last edited by John_Betong; Apr 2, 2012 at 06:49. Reason: I think I am on DD's Deleted List, maybe better to delete post
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  8. #8
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by John_Betong View Post
    I use plurals for arrays because there is more than one and makes the following line more readable.

    PHP Code:

      
    foreach( $items as $id => $item ):

         
    $echo $id ' ==> ' $item '<br />';

      endforeach; 
    for @John_Betong ; 's answer because this way of doing a foreach loop is much easier to read and understand the array from the value and key.
    ictus==""

  9. #9
    SitePoint Member
    Join Date
    Apr 2012
    Location
    /dev/zero
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    2.) Plural
    Code:
    $answers();
    $names();
    I like the style of plural for these types of variables

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep, plurals here too.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yes, plurals here as well. For the same "plurals to singular" reasons as stated above. Makes the code read like its a short sentence.
    PHP Code:
    foreach($kittens as $kitten){

       if(
    $kitten->isHungry())
          
    feed($kitten);




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •