views:

247

answers:

3

I tried to put an if statement inside an echo but this parse error came up, is it not possible to do that? should I use heredoc instead?

echo "<input name='main_branch' type='radio' value='1' <?php if($restaurant['main_branch'] == 1) { echo "checked"; } ?> />Yes
<input name='main_branch' type='radio' value='0' <?php if($restaurant['main_branch'] == 0) { echo " checked"; } ?> />No";
A: 

You probably want to separate it for readability anyway, something like this:

<?php

    echo "<input name='main_branch' type='radio' value='1' ";

    if($restaurant['main_branch'] == 1) { echo "checked"; } 

    echo " />Yes"
        ."<input name='main_branch' type='radio' value='0' ";

    if($restaurant['main_branch'] == 0) { echo " checked"; } 

    echo " />No";

?>
jaltiere
and it's been a while since I've written any PHP, but I was never a big fan of heredoc.
jaltiere
Thanks jaitiere for your reply :)
+1  A: 

You can't put <?php .. ?> inside an echo statement. You need to either set a variable outside and include it like echo "<input... $checked>"; or use the <?php tags.

webdestroya
Thank a lot webdestroya
No problem, happy to help
webdestroya
A: 

webdestroya has pointed out the problem, jaltiere has given one solution, I want to give another solution using PHP's embedded capabilities.

<input name="main_branch"
       type="radio"
       value="1"
       <?php if ($restaurant['main_branch'] == 1): ?>
           checked="checked"
       <?php endif; ?>
/> Yes

<input name="main_branch"
       type="radio"
       value="0"
       <?php if ($restaurant['main_branch'] == 0): ?>
            checked="checked"
       <?php endif; ?>
/> No
erisco