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.

Community
  • 1
  • 1
Wilest
  • 1,820
  • 7
  • 36
  • 62
  • 3
    do you have a constant named A? – mishu Nov 22 '11 at 09:34
  • You're missing the closing brackets and you're missing layout. – markus Nov 22 '11 at 09:38
  • 2
    instead of trying to write all your code in a single line , you should try to reduce the cyclomatic complexity. – tereško Nov 22 '11 at 09: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. – Corbin Nov 22 '11 at 09:45
  • @mishu I've checked but no constant named A – Wilest Nov 22 '11 at 10:10
  • @teresok. People might not understant what is cyclomatic complexity... Here is the link http://en.wikipedia.org/wiki/Cyclomatic_complexity – iWantSimpleLife Nov 22 '11 at 10:19

2 Answers2

4

Try this

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

Put () around your condition.

Jake N
  • 10,535
  • 11
  • 66
  • 112
  • Essentially the same answer as the other guy, but this is easier to read! – Dan Hanly Nov 22 '11 at 09:39
  • 1
    Yeah, line breaks always help – Jake N Nov 22 '11 at 09:41
  • Can the two people who down voted please explain their actions? – Jake N Nov 22 '11 at 09:42
  • 1
    Not a solution here. For such a simple condition, `()` are not mandatory, just a little better for visibility. `''` around litteral value, though, are. – psycho Nov 22 '11 at 09:43
  • (But I agree with the linebreak!) – psycho Nov 22 '11 at 09:45
  • 1
    @psycho It is not mandatory, but it matters. (Check this)[http://codepad.org/IcXA12zY]. – kapa Nov 22 '11 at 09:49
  • @psycho, please be consistent and down vote the other answer then too by zuloo. Also, if you down vote say why. Otherwise your doing no one any favours at all. – Jake N Nov 22 '11 at 09:51
  • @jakenoble : I was explaining my downvote just when you asked to explain. And I withdrew my vote since you added the quotes. But why should I have downvoted zuloo? His answer was correct (with quotes, I mean), even if not easily readable! – psycho Nov 22 '11 at 09:54
  • @psycho, a good point. I am going to blame the early morning and the vagueness of the OP's question :-) – Jake N Nov 22 '11 at 09:58
  • @jakenoble I've done exactly what you sugested, but no luck. It does not make any sense to me why it is not working. I'll take it from scratch and report back. – Wilest Nov 22 '11 at 10:17
  • 1
    @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. – psycho Nov 22 '11 at 10:23
  • As I said under the other answer, both questions share a remaining problem. Oh, and this was [my link](http://codepad.org/IcXA12zY). – kapa Nov 22 '11 at 10:40
  • @bazmegakapa Obviously, these ones matter! I was just speaking about *inside* parenthesis around condition (`$anscho1 == 'A'`), not those around the whole ternary statement! – psycho Nov 23 '11 at 10:09
4
     echo "<input type='radio' name='$quenr1' value='A'".(($anscho1 == 'A')? ' checked="checked"' : '').">$quenr1<BR>";

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

zuloo
  • 1,310
  • 1
  • 8
  • 12