dongyi2993 2016-02-19 06:45
浏览 81
已采纳

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-19 06: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条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行