PHP Help for Code

  • alexislalas
  • Novice
  • Novice
  • alexislalas
  • Posts: 32

Post 3+ Months Ago

Hello,

What is the logic behind the following code?

PHP Code: [ Select ]
<?php echo $st['puntos']>0?$st['puntos']:'0';?>


Thanks!!!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

basically:
PHP Code: [ Select ]
<?php
if($st['puntos']>0){
  echo $st['puntos'];
} else {
  echo '0';
}
?>
 
  1. <?php
  2. if($st['puntos']>0){
  3.   echo $st['puntos'];
  4. } else {
  5.   echo '0';
  6. }
  7. ?>
  8.  


see here
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Keep in mind that the shorthand if/else statement is to do simple comparison stuff rather than calling on functions or something like that.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Bogey wrote:
Keep in mind that the shorthand if/else statement is to do simple comparison stuff rather than calling on functions or something like that.

Hmm... where did you read that?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

It looks like it's designed to ignore negative numbers.

It could also be written as follows.
PHP Code: [ Select ]
<?php echo max(0, $st['puntos']); ?>

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.