Joomla / PHP中JRequest :: getVar($ var)和$ var之间的区别?

Let me start by saying I know nothing about Joomla or PHP so i might be comparing apples to oranges here but....

I keep seeing JRequest::getVar($var) and $var in some code i've been looking at. Can someone explain what the difference between these two is?

According to Joomlas' documentation getVar , "Fetches and returns a given variable." If that is the case why would the following code return different results?

echo JRequest::getVar($amount);

echo $amount;

Disclaimer: I don't know the first thing about Joomla.

Looking at the documentation, JRequest::getVar fetches values from GET or POST parameters and is supposed to be used like JRequest::getVar('amount') (note the argument is a string, not a variable). echo $amount and echo JRequest::getVar($amount) are of course nowhere near equivalent, the former means "echo the value of $amount", the latter "echo the value of a GET or POST variable with the name of the value of $amount."

The second parameter $default is also very useful:

$amount = JRequest::getVar('amount', 100);

// is roughly equivalent to:

if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['amount'])) {
    $amount = $_GET['amount'];
} else if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['amount'])) {
    $amount = $_POST['amount'];
} else {
    $amount = 100;
}

Even without the second parameter though, it'll take care of variable cleaning and (un)escaping where necessary, so it's a good idea to use this function to read request parameters.

getVar gets a variable from one of PHPs global arrays depending on the hash argument passed.

Joomla API

Code