doubledee Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/ Share on other sites More sharing options...
requinix Posted April 2, 2012 Share Posted April 2, 2012 I think you should use whatever you want to use. Just do it consistently. Link to comment https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333443 Share on other sites More sharing options...
l0gic Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333444 Share on other sites More sharing options...
doubledee Posted April 2, 2012 Author Share Posted April 2, 2012 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. Okay, but please answer my original question! Debbie Link to comment https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333447 Share on other sites More sharing options...
l0gic Posted April 2, 2012 Share Posted April 2, 2012 Okay, but please answer my original question! I did..? Link to comment https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333448 Share on other sites More sharing options...
doubledee Posted April 2, 2012 Author Share Posted April 2, 2012 Okay, but please answer my original question! I did..? I was asking about these 3 choices... $answer(); $answers(); $answerList(); I guess your style is like the last. Debbie Link to comment https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333449 Share on other sites More sharing options...
l0gic Posted April 2, 2012 Share Posted April 2, 2012 If it's an array? $arrAnswers Link to comment https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333450 Share on other sites More sharing options...
KevinM1 Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333451 Share on other sites More sharing options...
Adam Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333478 Share on other sites More sharing options...
scootstah Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333524 Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333539 Share on other sites More sharing options...
.josh Posted April 2, 2012 Share Posted April 2, 2012 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 https://forums.phpfreaks.com/topic/260166-array-name-singular-or-plural/#findComment-1333541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.