28

I ask you, native English speakers:

What is the correct name for a variable which contains ids of multiple users (from grammar point of view):

A) users_ids vs B) user_ids

I'm pretty sure C) users_id is wrong.

The variable is an array of ids:

array(12, 43, 12, 53)

and why?

3
  • 1
    $user_ids is not bad at all. Not much sophisticated grammar involved, an array of user ids. Apostrophes not included for syntactic reasons. Aug 25, 2011 at 22:59
  • Do those ids belong to a single or multiple users? When I saw a title I decided it is about multiple users. This may be something others understood as well. But it is not mentioned explicitly. Could you please clarify it? Nov 16, 2020 at 11:26
  • 1
    @VictorYarema multiple users.
    – GoTo
    Nov 24, 2020 at 8:42

7 Answers 7

32

$user_ids because it refers to a list of ids (plural), each belonging to a single user (singular). When looping through the ids I often use something like the following:

$user_ids = array(12, 43, 12, 53);

foreach($user_ids as $user_id) {
// at this point $user_id refers to one id for one user
}

Besides, $user_ids is the most commonly used form (that I've seen).

1
  • What if table contains id_page? Should its plural be ids_page, id_pages or ids_pages? Feb 22, 2021 at 6:24
7

$user_ids is the most common. My reasons are:

  • you have multiple ids so users_id is incorrect
  • users_ids sounds awkward
  • PHP variables generally do not use camelCase
0
5

Grammatically, userIds or user_ids would be most familiar to English speakers.

However- you'll often see something like usersId if it refers to a column id on a table users, especially as it relates to foreign key relationships.

Consider:

orders.id

orders.users_id <-> users.id

As it pertains to an array of user IDs that doesn't really 'translate' to a specific object relationship in your data store, $userIds. (or $user_ids if you insist ;)

0
5

user_ids - you have not one user_id but a few.

users_id - many users have a same ID (you are in trouble if that happens...).

0
4

I think $users_ids, because your variable contains multiple identifiers of multiple users. If the variable would contain multiple identifiers for a single user must use $user_ids.

1

Does it matter? As long as the variable name conveys to the reader what its data represents, that's all that really matters..

Well, that and the variable name conforms to your coding standards (if applicable).

(on a side note, I'd most likely name it $userIds)

2
  • 1
    "conveys to the reader what its data represents" I'm trying to set a standard for myself for future work; the reader could be anybody so why not make it perfect. "your coding standards" are a work in progress.
    – GoTo
    Aug 26, 2011 at 0:24
  • As you can see here already, there are varying opinions (using/not using underscores, etc). "Perfect" is (to an extent) subjective and therefore isn't really worth striving for. If you're really worried about how everyone else is doing it, maybe take a look at pear.php.net/manual/en/standards.php (although they don't have internal variable naming conventions listed either apparently) Aug 26, 2011 at 0:32
1

Let’s assume we have students and teachers. I want to keep in database which teachers particular student have seen

In that case for me student.seen_teachers_ids or student.teachers_ids make more sense

for (teacher_id in teachers_ids)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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