dss087358 2010-11-04 02:43
浏览 79
已采纳

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;
  • 写回答

2条回答 默认 最新

  • douyo770657 2010-11-04 03:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含