RE: [PHP] Seems Simple enough
| From: | Ford, Mike [LSS] | Date: | Tue, 27 Jan 2004 11:21:06 +0000 |
| Subject: | RE: [PHP] Seems Simple enough | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-175884@lists.php.net to get a copy of this message | ||
On 26 January 2004 16:56, Christopher J. Crane wrote:
> Ok here is the wierd thing.
> I pasted more code, it seems to not work because of me
> changing the number
> format.
>
> This works ...
> if($Balance >= 10001) {
> $Balance = number_format($Balance,2,'.',',');
> echo "<font color=\"green\">\$$Balance</font><br>\n";
> }
> if($Balance <= 9999) { $Balance =
> number_format($Balance,2,'.',','); echo "<font
> color=\"red\">\$$Balance</font><br>\n"; } else { $Balance =
> number_format($Balance,2,'.',','); echo "<font
> color=\"purple\">\$$Balance</font><br>\n"; }
>
> This does not ...
> $SummaryResults = mysql_query("SELECT * FROM Accounting WHERE
> UserID='$UserID' LIMIT 1") or die("Invalid query");
> while($SummaryField = mysql_fetch_array($SummaryResults)) {
> $Balance = number_format($SummaryField["Balance"],2,'.',',');
$Balnace is now (for example) "10,000.00"; comma is not a valid character in a PHP number,
so when interpreted as a number this will convert to 10.
> } if($Balance >= 10001) { echo "<font
> color=\"green\">\$$Balance</font><br>\n"; }
> elseif($Balance <= 9999) { echo "<font
10 is <= 9999 so...
> color=\"red\">\$$Balance</font><br>\n"; }
> else { echo "<font
> color=\"purple\">\$$Balance</font><br>\n"; }
But I have to ask, why include all the invariant parts in every branch of your if-else chain? As a
general principle, I try to put only the parts that are genuinely different inside conditionals, so
in this case I'd probably write something like:
echo '<font color="';
if ($Balance >= 10001):
echo 'green';
elseif ($Balance <= 9999):
echo 'red';
else:
echo 'purple';
endif;
echo '">$', number_format($Balance, 2, '.', ','),
"</font><br />\n";
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211