Reputation: 13
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
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
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