dongyi2993 2016-02-18 22:45
浏览 82
已采纳

PHP:使用表达式初始化数组

I have a simple (in my opinion) question. I try to initialize an array in php as follows:

array(
  'type' => 'hidden',
  'id' => "request_params$suffix",
  'name' => "request_params$suffix",
  'value' => "?rule_id=$rule_id&cur_instr=$cur_instr&dev_id=$dev_id&cmd_id=$cmd_id" + ($disabled? '&disabled' : ''))

This construction is passed as a parameter in a function call. All of the variables are defined. And as a result I get the type, id and name are initialized well, but the value is initialized with 0. If I comment out the + ($disabled? '&disabled' : '') then the value initialized too. I had tried to enclose all the expression in parenthesis with the same result -- initializing with 0.

Has anybody any idea?

  • 写回答

3条回答 默认 最新

  • dskld5423 2016-02-18 22:50
    关注

    You are using arithmetic operator for concatination of two strings i.e. + instead of .

    array(
      'type' => 'hidden',
      'id' => "request_params$suffix",
      'name' => "request_params$suffix",
      'value' => "?rule_id=$rule_id&cur_instr=$cur_instr&dev_id=$dev_id&cmd_id=$cmd_id" . ($disabled? '&disabled' : ''))
    

    Because you are using+ its trying to add numbers and because it is unable to find that its assuming both variables as 0 so 0+0=0.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部