Jump to content


This topic is now archived and is closed to further replies.


if syntax problem

Recommended Posts

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";

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 :)

<td><a href="nompen.php">nompen </a><b>(<font color=<?php $numcolor?>><?php echo $nompen?></font>)</b></td>[/code]

Share this post

Link to post
Share on other sites
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:
echo '<td><a href="nompen.php">nompen </a><b>(<font color="' . $numcolor . '">' . $nompen . '</font>)</b></td>';
You can also use the ternary operator instead of the if-else statement:
$numcolor = ($nompen > 0)?"#FF0000":"#333333";
This operator is described about 1/2 way down [url=http://www.php.net/manual/en/language.operators.comparison.php]this page[/url]


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.