Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I'm thinking of some of the array functions. "key", "each", "pos", "range". These are often very useful as local identifiers! I have also seen code that (ab)uses $return, $list, $array, $string. What are other PHP programmers views on this?

Local identifiers only! I wouldn't use them as members or method names of a class, even less global variables.

share|improve this question

closed as not a real question by Jim G., gnat, Dynamic, Walter, GlenH7 Dec 20 '12 at 13:58

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

5 Answers 5

"key", "each", "pos", "range". These are often very useful as local identifiers!

Disagree. They are too generic as pointed by another answer. To generic variable is not a good variable, unless the program is one page long.

The good thing is PHP allows them to be used as variables and do not give you some strange error as in .NET. If php is intellegent enough to know what is a variable and what is not, it is a good thing.

Should I use it as variable or function names?

The whole purpose of writing good code is that it should be clean, easy to read and maintain and with less confusion. You would be adding to confusion if you use reserved keywords as variable names and function names. Keeping in traditions with other languages, it is a bad practice to use keywords as variable names etc. In short, you should be consistent, do not use keywords purpose other than keywords (and also to comply with other languages), but if you want to, you can.

Here is a quick program that I wrote which is perhaps only possible in PHP :)

$n1= 2;
$n2= 5;

echo "The sum of $n1 and $n2 is ".addnumbers($n1,$n2);

function addnumbers($a, $b)
  $return = $a+$b;

  return $return;
share|improve this answer
That concept should work in any language that uses identifiers for their variables, which includes (at least) Perl and bash as well as PHP. – Izkata Mar 9 '12 at 22:09

The reason they bother me is not because they are reserved words within the language itself, but rather that they are too general. What does the $array contain? What is it an array of?

$list, $string, $array, $key, etc are far too generic and not descriptive enough variable names to be useful. Especially when debugging someone else's code.

Even in the context of local variables, having more descriptive variable names would make understanding what the code is doing much much faster.

share|improve this answer
I agree in general, except that $key can be a reasonable variable name in a short loop over an associative array. – Alexander Corwin Mar 9 '12 at 20:23
That is a fair point. But sometimes a descriptive name is hard to come up with. When I read other peoples' C code, for instance, I'm much more comfortable with for(i=;...) than for(network_graph_vertex_index;...). – Emanuel Landeholm Mar 11 '12 at 11:35

I don't see any problem with (ab)using identifiers like $key, $array, $return, etc.

The fact is, $key is not the same symbol name as key(). $namespace is not the same as namespace: they don't write the same, and they don't mean the same. I'd say this is a nice feature of PHP, and Perl too.

It clearly demonstrates your intention:

$string = 'yay';   // I'd be very surprised if referencing this returned a float

$array[] = 'baz';  // I know that it is an array, and not a ArrayAccess object, etc.

$key = 'bar';      // I know this refers to an array key, or a cache key, or more generally 
                   // some sort of unique identifier

$return = 'hello'; // I know this is going to be returned at some point
share|improve this answer
I find the idea of naming a variable "array" rather scary. It would have to be very clear in that content (like having function linkedListToArray). The same applies for "string". – Matěj Zábský Mar 9 '12 at 17:00
I'm not sure about the second part of your comment. Would you care to elaborate? – netcoder Mar 9 '12 at 17:01

Using names such as $key, $value and $list is such a common practise when iterating array that it could be seen to be part of the PHP way.

One new thing to consider. Although not extensively used in PHP, the language has supported a form of closure/anonymous function since 5.3. As functions can thus be assigned to variables, it would be sensible to avoid some of the more confusing combination.

For example, node developers will often use "next" for a return function which is an existing array method. Similarly, if a closure ended with $return(); instead of return; that may also confuse. Using $callback which has no equivalent would seem safe.

In general, names such as $list, $array, $string or $number, shouldn't be used except for the innermost loops, as they only reflect type rather than purpose.

Regarding method names for a class, if the class has array type behaviours it would be completely appropriate to use them, or even enforce it using the ArrayIterator or ArrayAccess interfaces.

share|improve this answer
I hadn't thought about the SPL interfaces... Good point. Novelty account, can't upvote! – Emanuel Landeholm Mar 9 '12 at 5:10

From your perspective it may seem like they 've stolen your favorite symbol names - and in many occassions this can be said for most programming languages. From a seasoned PHP developer's viewpoint however, these symbols are a solid foundation on which he can be based to produce code with a well - defined behavior - at least to the extent that these functions have a well - defined behavior.

I personally consider it very distasteful that PHP 's reserved word space is extended with so many common words but this decision was made well into the past, and so it 's now part of the language 's actual as well as conceptual vocabulary, more or less.

Besides that, I think that programming is as much about coding instructions as it is about writing, especially given that other programmers may need to review and maintain your code, and that you yourself may come to forget that particular piece of code you may have written a few months back. In that vein, well - named symbols immensely increase the readability of your code, and by extension help everyone working on it.

It may take a little more work and creativity, but there 's nothing more rewarding than a handsomely crafted piece of code which can be read and understood with minimal effort.

share|improve this answer
I agree that programming is (or should be) about writing. And writing is, in the end, about reading. Code that doesn't read well is tolerable for computer generated stuff which is later translated and which no human ever needs to eyeball. Would upvote... – Emanuel Landeholm Mar 9 '12 at 5:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.