/    Sign up×
Community /Pin to ProfileBookmark

Quick Noob Syntax Questions

Hi I’m very new to PHP and I’m have a small problem.

I have my grandtotal variable displaying using the code below

[code]echo “<tr><td>Grand Total:”.$grandtotal.”</td></tr>”;[/code]

but I need it to round up to have only 2 decimal places it seems to work when i try this

[code]echo (number_format(“$grandtotal”, 2));[/code]

but I need it to be placed inside my table and I’m unsure of the syntax i’ve tried something like this with no luck.

[code]echo “<tr><td>Grand Total:”(number_format($grandtotal,2))”</td></tr>”;[/code]

can someone help me out with the syntax on this one?

thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 15.2009 — Just replace those outer parentheses with the "." concatenation character:
[code=php]
echo "<tr><td>Grand Total:" . number_format($grandtotal,2) . "</td></tr>";
[/code]

There are a number of other alternatives, such as printf():
[code=php]
printf("<tr><td>Grand Total: %.2f</td></tr>", $grandtotal);
[/code]

Or exiting out of PHP mode for the text, then back in for the PHP value:
[code=php]
?>
<tr><td>Grand Total: <?php echo number_format($grandtotal,2); ?></td></tr>
<?php
// back into PHP mode if needed
?>
[/code]
Copy linkTweet thisAlerts:
@allanice001Oct 15.2009 — Personally i prefer using single quotes.
[code=php]echo '<tr><td>Grand Total:' . number_format($grandtotal,2) . '</td></tr>';[/code]
Copy linkTweet thisAlerts:
@NogDogOct 15.2009 — Personally i prefer using single quotes.
[code=php]echo '<tr><td>Grand Total:' . number_format($grandtotal,2) . '</td></tr>';[/code][/QUOTE]


There is certainly nothing wrong with single quotes in this case, since there is no need for variable interpolation within the quoted string literal. However, unless you are using a very old version of PHP where there was some measurable performance gain for using single quotes when no such interpolation was needed, it's purely a style/taste preference. I tend to use double quotes by default so that I can include interpolated varaibles and/or use any of the special escape sequences if/when I need them.
×

Success!

Help @cultivate spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...