dstxpei5823 2017-08-02 14:49
浏览 21
已采纳

php如何解析url参数?

I have a url

www.example.com?para1=&para2=2

I try to judge the para1 and para2 ;

isset($_GET['para1'] )       // true
 isset($_GET['para2'])       //true;
 isset($_GET['para3'])       // false

I think that isset($_GET['para1'] ) is false ,but it seems not .

My problem :

why isset($_GET['para1'] ) is true.How it parse the url in php?

  • 写回答

6条回答 默认 最新

  • doutu1939 2017-08-02 15:02
    关注

    A lot of people here are saying the value of a $_GET['para1'] is null but that is not the case. There is a comment on the php.net $_GET docs that provides a useful script to test this.

    Given the URL: http://www.example.com?a

    You can use this script to test results:

    <?php
    print_r($_GET);
    if($_GET["a"] === "") echo "a is an empty string
    ";
    if($_GET["a"] === false) echo "a is false
    ";
    if($_GET["a"] === null) echo "a is null
    ";
    if(isset($_GET["a"])) echo "a is set
    ";
    if(!empty($_GET["a"])) echo "a is not empty";
    ?>
    </pre>
    

    The output would be:

    a is an empty string
    a is set
    

    This is the case because a key without a value is actually an empty string not null

    So to directly answer your question:

    Even though it does not appear para1 has a value set, it actually does. The value is an empty string (""). Even though this value is falsy, it is still a value and as such, isset() returns true. You can use !empty() to return false on empty strings. See a comparison of isset() empty() and is_null() here.

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)