views:

98

answers:

3
+1  Q: 

PHP functions help

Hi there! I'm sorry for asking this question, but I'm not good in php (beginner). Could you please explain what $arg means in this piece of code? (it's from one of drupal modules)

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

Thank you.

+5  A: 

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:

node_add_review_load("my argument");

//and the php automatically does:

$arg = "my argument";

//before executing the rest of the function.
Time Machine
A: 

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 same node_id (i.e., $arg); otherwise it will return FALSE.

VoteyDisciple
Well, it is a helpful name. When you give an opinion, you must give an argument. So basicly: I use node_add_review_load, because I $arg.
Time Machine
A: 

It's an argument.

Example,

// function
function sum($arg1, $arg2)
{
 return $arg1+$arg2;
}

// prints 4
echo sum(2,2);

You don't have to call it $arg for it to be valid. For example,

function sum($sillyWilly, $foxyFox)
{
 return $sillyWilly+$foxyFox;
}

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.

Daniel