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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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..? Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
l0gic Posted April 2, 2012 Share Posted April 2, 2012 If it's an array? $arrAnswers Quote Link to comment 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. Quote Link to comment 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) { Quote Link to comment 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. Quote Link to comment 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(); Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.