duanpanhuo0618 2018-08-04 22:14
浏览 625
已采纳

PHP:list()在PhpStorm中给出未使用的局部变量错误

I am using the answer found at https://stackoverflow.com/a/25749660 in order to sort the $_SERVER['HTTP_ACCEPT_LANGUAGE'] array by the most preferred language.

In that answer (which is working great by the way), one line is:

list($a, $b) = explode('-', $match[1]) + array('', '');

Within PhpStorm, I get the following error for that line:

"Unused local variable $b: The value of the variable is overwritten immediately".

I'm a little confused as to what this line is doing exactly, so I don't know if I should just keep it the same, or if I should modify it to:

list($a) = explode('-', $match[1]) + array('', '');

... which also seems to be working fine.

Should it be changed?

  • 写回答

2条回答 默认 最新

  • duanjie5570 2018-08-04 22:29
    关注

    You can't join arrays with the arithmetic operator +. Basically you are telling PHP to convert the arrays to scalar types and then sum them, which yields you a number (probably arrays evaluate to 1 if it has elements and 0 if it doesn't).

    The result is that effectively you are doing something like:

    list($a, $b) = 2;
    

    And the conclusion PHP reaches is that you haven't specified enough elements to define all the variables in the list.

    To join two arrays together, use array_merge().

    list($a, $b) = array_merge(explode('-', $match[1]), array('', ''));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题