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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容