Noctagon Posted September 27, 2006 Share Posted September 27, 2006 Hi all, probably just a noob with syntax error again. Can anyone spot what is wrong in my attempt to set up an if statement based on the value of a variable.Thanks :)[code] if ($nompen > 0) { $numcolor = "#FF0000"; } else { $numcolor = "#333333"; }[/code]I am trying to use it here to specify font colour based on value of $nompen which I can echo and it is giving a number :)[code]<td><a href="nompen.php">nompen </a><b>(<font color=<?php $numcolor?>><?php echo $nompen?></font>)</b></td>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 27, 2006 Share Posted September 27, 2006 Your problem lies in this statement:[code]<td><a href="nompen.php">nompen </a><b>(<font color=<?php $numcolor?>><?php echo $nompen?></font>)</b></td>[/code]You need to used "echo $numcolor", not just "$numcolor". So it would be:[code]<td><a href="nompen.php">nompen </a><b>(<font color=<?php echo $numcolor?>><?php echo $nompen?></font>)</b></td>[/code]You could shorten this a little by echoing the whole line from PHP:[code]<?phpecho '<td><a href="nompen.php">nompen </a><b>(<font color="' . $numcolor . '">' . $nompen . '</font>)</b></td>';?>[/code]You can also use the ternary operator instead of the if-else statement:[code]<?php$numcolor = ($nompen > 0)?"#FF0000":"#333333";?>[/code]This operator is described about 1/2 way down [url=http://www.php.net/manual/en/language.operators.comparison.php]this page[/url]Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.