开发者

correct name for a variable users_ids vs user_ids

I a开发者_如何学Gosk 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?


$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).


$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


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 ;)


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...).


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.


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)


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜