Jump to content

if syntax problem


Noctagon

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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.