saad nasim
saad nasim

Reputation: 13

ternary operator in array

I am trying to use the ternary operator in a string in PHP

"hard_copy"=>"<input type='checkbox' (($station->hard_copy==1)?'checked':'') name='station[]' id='hard-copy-$key' class='hard-copy' value='hardcopy-$station->id-1' >
<label for='hard-copy-$key' style='margin-left: 30%;'></label>",

but I am having probably some syntax error

Upvotes: 1

Views: 238

Answers (2)

S.Sachith
S.Sachith

Reputation: 596

I Will answer as I understand the question
as @Magnus Eriksson mentioned " You can't unpack statements inside double-quoted strings like that, only variables. You need to use concatenation. "
try this one

"hard_copy"=>
"<input type='checkbox' ". (($station->hard_copy==1) ? 'checked' : '') . "name='station[]' id='hard-copy-$key' class='hard-copy' value='hardcopy-$station->id-1' >
<label for='hard-copy-'".$key." style='margin-left: 30%;'></label>",

Upvotes: 1

Harshit Rastogi
Harshit Rastogi

Reputation: 2122

Try this using concatenation:

"hard_copy"=> "<input type='checkbox' ". ($station->hard_copy==1 ? 'checked':'' )." name='station[]' id='hard-copy-".$key."' class='hard-copy' value='hardcopy-".$station->id."-1' ><label for='hard-copy-".$key."' style='margin-left: 30%;'></label>",

Upvotes: 0

Related Questions