pepe pepe - 5 months ago 13
PHP Question

How to mix PHP with a string inside CodeIgniter function?

I would like to dynamically set the

id
for each iteration of this radio button within a loop.

However have not been able to figure out the correct way of escaping strings and variables in the
id
portion. The quotes in that portion are failing to parse.

<?php
echo form_radio($data['filename'], 0, '', 'class="uniform" id="'$data['ids']'");
?>


This uses the
form
helpers from CodeIgniter which have this layout.

Any idea how to solve this?

Answer

You must start with double quotes to get expanded variables, then escape nested ones with backslash

<?php 
echo form_radio($data['filename'], 0, '', "class=\"uniform\" id=\"$data[ids]\""); 

Or use single quotes inside double

<?php 
echo form_radio($data['filename'], 0, '', "class='uniform' id='$data[ids]'"); 

Notice you don't need to quote single dimensional array indexes inside the square braces if inside double quotes

Comments