views:

102

answers:

5

I have this code:

<?php
if ( $amount < 5 ) {
  echo 'Credit Balance low! You have';
  echo $amount;
  echo ' remaining credits.';
} else {
  echo 'No recent alerts...';
}
?>

Where it says echo $amount; I want to echo $00.00 if the value of $amount == 0. How would I include this in my code?

+3  A: 
echo $amount === 0 ? '$00.00' : $amount;

?

cletus
+1  A: 
if ($amount === 0) {
    echo '$0.00';
} elseif ($amount < 5) {
    echo "Credit Balance low! You have $amount remaining credits";
} else {
    echo 'No recent alerts...';
}
SilentGhost
You have a rogue echo in the middle part - it should probably be `echo 'Credit Balance low! You have ', $amount, ' remaining credits';`
Chris Smith
@Chris: thanks, fixed
SilentGhost
`echo 'Credit Balance low! You have ', $amount, ' remaining credits';` is also the fastest way of echoing that line.
Dan Heberden
@Dan: If it's faster, it's negligible and not even worth mentioning. I'm pretty sure most people would prefer the more readable double quote version.
ryeguy
@ryeguy - using vars in double quotes in itself is overhead (doubles are slower than single quotes because the WHOLE string has to be parsed for a var) as well as multiple calls. Sure, it's not a huge difference but isn't that what best practice is all about? Isn't it not worth pointing out to those interested? Too, for readability, I personally find vars shoved in quotes hard to find when reviewing code. 'text label for ', $myVar makes myVar easy to spot. Also, what about when arrays are used? $data['object']['label'] - why mix up encoding styles of double quotes with vars to concatenation?
Dan Heberden
@Dan: I've done timings a while ago, and NO there isn't any performance penalty. Using decent text editor/IDE would let you *spot* your variable with ease. In case of array elements, it might be more beneficial to use string concatenation, but in this particular case the best practice would be to leave the variable in double quotes.
SilentGhost
In the interest of healthy debate :) http://fusionswift.com/2010/05/php-concatenation-benchmark-comma-vs-period/ - And yes, i know it's over a gagillion tries :/ I just felt if worth mentioning, i still +1'd the answer :p (and i know editors display the var, i suppose my brain just has a better time with them outside quotes so that way if there is anyone on here with crazy brains like me, they'll have a tip heh heh)
Dan Heberden
@Dan: You can still parse objects and arrays inside of quotes if you wrap them in curly braces.
ryeguy
Yeah, it's all the readability thing for me - but this is good, I think anyone stumbling on this post is going to find out the four hundred ways to output data, lol
Dan Heberden
+4  A: 

You're probably looking for the money_format() function.

<?php echo money_format('%=0-2', $amount); ?>

=0 fills with the 0 character, and -2 left-justifies to a minimum field width of 2.

Dolph
A: 
<?php
if ( $amount < 5 )
{
    echo 'Credit Balance low! You have';
    echo $amount == 0 ? '$00.00' : $amount;
    echo ' remaining credits.';
}
else
{
    echo 'No recent alerts...';
}
?>
klez
Thanks dude thats great
@user34: that's just pathetic
SilentGhost
Cletus posted this answer 3 minutes before klez..
Dolph
if I could un-accept this myself I would. I'll award a +1 to cletus for correctness.
klez
A: 

You should do:

<?php 

if ($amount === 0) {
  echo '$00.00';
}
elseif ($amount < 5 && $amout > 0) {
  echo 'Credit Balance low! You have' . $amount . ' remaining credits.';
}
else {
  echo 'No recent alerts...';
}    

?>
Sarfraz