duanluanlang8501 2012-05-04 15:54
浏览 18
已采纳

引用没有值的PHP变量

I just transferred a PHP website from a Linux server to Windows. Everything seems to function the same except one huge difference:

On the Linux server, the following piece of code is ignored when $_GET['action'] is not set:

$action = $_GET['action'];
if($action=='add'){
    echo 'good';
}

However, this prevents the page from loading on the Windows server.

What does work is:

if(isset($_GET['action'])){
    $action = $_GET['action'];
}else{
    $action='';
}

if($action=='add'){
    echo 'good';
}

2 questions:

  1. Is there a way to configure the server to be more forgiving of variables that don't have a value?

  2. Is the second code example better practice?

  • 写回答

3条回答 默认 最新

  • dousuohe5882 2012-05-04 15:57
    关注
    Is there a way to configure the server to be more forgiving of variables that don't have a value?
    

    putting an @ symbol infront of the variable will tell php to ignore the error. but i consider this as bad practice.

    Is the second code example better practice?
    

    i do consider it as a good practice, it is always good to check.

    IMO you should go for second, you can always use a ternary operator. so instead of

    if(isset($_GET['action'])){
        $action = $_GET['action'];
    }else{
        $action='';
    }
    

    you can write

    $action = isset($_GET['action']) ? $_GET['action'] : '';
    

    both does the same thing.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号