Jump to content

Archived

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

Noctagon

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";
}[/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]

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:
[code]<?php
echo '<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

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.