dongzongpeng6474 2014-04-17 17:56
浏览 20
已采纳

修复遗留代码而不破坏所有内容

I have some code like this :

somefunc($row['some_key']);

Sometimes, $row might not have 'some_key' which logs an Undefined index warning.

How do you fix this warning without changing the current behavior of the program ?

I know I need to fix the problem to the source but this is a legacy codebase T_T

My attemp

if(!isset($row['some_key'])){
    somefunc(null);
}
else{
    somefunc($row['some_key']);
}

Is this equivalent ?

  • 写回答

3条回答 默认 最新

  • dozug64282 2014-04-17 18:01
    关注

    Sure, it is equivalent, but you can be more terse (if you're interested) with a ternary operator (http://www.php.net/manual/en/language.operators.comparison.php you have to scroll a little, unfortunate, they don't have any anchor for it)...

    somefunc( (isset($row['some_key'])) ? $row['some_key'] : null );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端