dongsuiying7773 2014-06-26 06:28
浏览 26
已采纳

PHP:如何获取无密钥URL参数

I would like to get the parameter in a key such as http://link.com/?parameter

I read somewhere that the key is NULL and the value would be parameter.

I tried $_GET[NULL] but it didn't seem to work.

Here is my code:

if ($_GET[NULL] == "parameter") {
    echo 'Invoked parameter.';
} else {
    echo '..';
}

It just prints out .. so that means I'm not doing it right. Can someone please show me the right way.

  • 写回答

5条回答 默认 最新

  • doulu1907 2014-06-26 06:32
    关注

    There are no such things as keyless URL parameters in PHP. ?parameter is the equivalent of $_GET['parameter'] being set to an empty string.

    Try doing var_dump($_GET) with a url like http://link.com/?parameter and see what you get.

    It should look something like this:

    array (size=1) 'parameter' => string '' (length=0)

    Thus, you can test it in a couple of ways depending on your application needs:

    if (isset($_GET['parameter'])) {
       // do something
    } else {
       // do something else
    }
    

    or

    // Only recommended if you're 100% sure that 'parameter' will always be in the URL.
    // Otherwise this will throw an undefined index error. isset() is a more reliable test.
    
    if ($_GET['parameter'] === '') {
       // do something
    } else {
       // do something else
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大