1

I've been struggling with this from yesterday. I'm trying to do an if statement in an echo, I've followed this post (if block inside echo statement?) but my if statement does not evaluate to true. I've even put in the values for $quenr1 & $anscho1 but no sucess. Please help. Please reply if you need all the code.

$quenr1 = 1;
$anscho1 = 'A';
if ($q_type != 'mr') 
{
    if($option1!="") 
    {
        echo "<input type='radio' name='$quenr1' value='A'" . ($anscho1 == A ? 'checked="checked"' : '') . ">$quenr1<br />";
    }
}

EDITED

Thanks for all the replies, you've put me in the right direction. As a last resort I removed all my code and started from scratch and just put in the query to retrieve the data and display the form and it worked fine.

6
  • 3
    do you have a constant named A? Commented Nov 22, 2011 at 9:34
  • You're missing the closing brackets and you're missing layout. Commented Nov 22, 2011 at 9:38
  • 2
    instead of trying to write all your code in a single line , you should try to reduce the cyclomatic complexity. Commented Nov 22, 2011 at 9:41
  • 2
    I suggest developing with error display on (or checking your error log), and error reporting setting to E_ALL. I'm assuming the A is not actually a constant but rather a literal missing quotes. Error reporting will tell you that. Commented Nov 22, 2011 at 9:45
  • @mishu I've checked but no constant named A Commented Nov 22, 2011 at 10:10

2 Answers 2

4
     echo "<input type='radio' name='$quenr1' value='A'".(($anscho1 == 'A')? ' checked="checked"' : '').">$quenr1<BR>";

right put () arround your condition and '' around the A

Sign up to request clarification or add additional context in comments.

3 Comments

Actually both answers are missing a space (between value and checked).
@bazmegakapa I've changed it to this but still no luck. echo "<input type='radio' name='$quenr1' value='A'".(($anscho1 == 'A')? 'checked="checked"' : '').">$quenr1<BR>";
@wilest Try it like this: ' checked="checked"'
4

Try this

echo "<input type='radio' name='$quenr1' value='A'" .
  (($anscho1 == 'A') ? 'checked="checked"' : '')
  . ">$quenr1<BR>";

Put () around your condition.

13 Comments

Essentially the same answer as the other guy, but this is easier to read!
Yeah, line breaks always help
Not a solution here. For such a simple condition, () are not mandatory, just a little better for visibility. '' around litteral value, though, are.
@psycho It is not mandatory, but it matters. (Check this)[codepad.org/IcXA12zY].
@wilest Instead of comments, consider using 'edit' function with new informations about your problem. jakenoble's answer is (now) right, so if it doesn't work, you probably forgot / misunderstood something or made a typo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.