Jump to content


Photo

if syntax problem


  • Please log in to reply
1 reply to this topic

#1 Noctagon

Noctagon
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 27 September 2006 - 01:51 AM

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

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>


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 September 2006 - 02:30 AM

Your problem lies in this statement:
<td><a href="nompen.php">nompen </a><b>(<font color=<?php $numcolor?>><?php echo $nompen?></font>)</b></td>
You need to used "echo $numcolor", not just "$numcolor". So it would be:
<td><a href="nompen.php">nompen </a><b>(<font color=<?php echo $numcolor?>><?php echo $nompen?></font>)</b></td>
You could shorten this a little by echoing the whole line from PHP:
<?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:
<?php
$numcolor = ($nompen > 0)?"#FF0000":"#333333";
?>
This operator is described about 1/2 way down this page

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users