函数声明后括号内有一个变量是什么意思?
您能解释一下这段代码中 $arg
的含义吗? (它来自 Drupal 模块)
function node_add_review_load($arg) {
global $user;
$add_review = FALSE;
$current_node = node_load($arg);
$type =$current_node->type;
$axes_count = db_result(db_query("SELECT COUNT(*) FROM {nodereview_axes} WHERE node_type='%s'", $type));
if (variable_get('nodereview_use_' . $type, 0) && $axes_count) {
$add_review = db_result(db_query("SELECT n.nid FROM {node} n INNER JOIN {nodereview} nr ON n.nid=nr.nid WHERE uid=%d AND reviewed_nid=%d", $user->uid, $arg));
}
return $add_review ? FALSE : $arg;
}
Could you please explain what $arg
means in this piece of code? (it's from a Drupal module)
function node_add_review_load($arg) {
global $user;
$add_review = FALSE;
$current_node = node_load($arg);
$type =$current_node->type;
$axes_count = db_result(db_query("SELECT COUNT(*) FROM {nodereview_axes} WHERE node_type='%s'", $type));
if (variable_get('nodereview_use_' . $type, 0) && $axes_count) {
$add_review = db_result(db_query("SELECT n.nid FROM {node} n INNER JOIN {nodereview} nr ON n.nid=nr.nid WHERE uid=%d AND reviewed_nid=%d", $user->uid, $arg));
}
return $add_review ? FALSE : $arg;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://nl.php.net/manual/en/functions.arguments。 php
当程序员使用node_add_review_load()时,他可以传递可以在函数中使用的参数。
如果参数 $arg 不同,该函数将返回另一个值。
所以程序员可以这样做:
http://nl.php.net/manual/en/functions.arguments.php
When a programmer uses node_add_review_load() he can pass the argument which can be used in the function.
The function returns another value if the argument $arg is different.
So the programmer can do this:
一般来说,
arg
是“argument”的缩写,如“函数的参数”。 这是一个通用的名称,因此毫无帮助。 如果您只是给出方法签名(function node_add_review_load($arg)
),我们将不知道。幸运的是,有了完整的函数体,我们就可以推断出它的用途:它就是node_id。 此函数加载由
$arg
标识的节点,然后尝试查找已加载的相应行,然后代码尝试查找当前用户的相应评论。 如果成功,该函数将返回相同的node_id
(即$arg
); 否则将返回FALSE。In general,
arg
is short for "argument," as in "an argument to a function." It's a generic, and thus unhelpful, name. If you'd just given the method signature (function node_add_review_load($arg)
) we'd have no idea.Fortunately, with the complete function body, we can deduce its purpose: it is the node_id. This function loads the node identified by
$arg
and then tries to find a corresponding row that's loaded, and that the code then tries to find a corresponding review for the current user. If successful, the function will return that samenode_id
(i.e.,$arg
); otherwise it will return FALSE.这是一个争论。
例如,
您不必将其命名为 $arg 才有效。 例如,
它的工作原理是一样的。 您应该为参数指定有用的名称。 在这种情况下,参数 $arg 是不好的编程习惯,因为像您这样的人会看到它并对其确切含义感到困惑。 因此,在创建函数时,请务必使用有用的名称,以便您记住。
It's an argument.
Example,
You don't have to call it $arg for it to be valid. For example,
And it would work the same. You should give arguments useful names. In this case, the argument $arg is bad programming practice because someone like you would look at it and get confused by what it means exactly. So in cases where you make functions, be sure to use useful names so you'll remember.