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?
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
.
To concatenate strings use the dot
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' : ''))
I think that code will help you alot...
$disabled='';
$suffix='suffix';
$array=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' : '')
);
var_dump($array);