using elseif in php

  • Dan030981
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Mass

Post 3+ Months Ago

Just curious if I'm using the elseif statement correctly. Here is a snippet of code that I'm using. The code here have a couple of elseif statements and each of them have the same echo out call. Is this the proper way of using it?

Code: [ Select ]
<script type="text/javascript">
<?php
$points = $karma->getUserPoints($user);

    if ($points == 1000):
elseif ($points == 2000):  
elseif ($points == 3000):
elseif ($points == 4000):
elseif ($points == 5000):
elseif ($points == 6000):
elseif ($points == 7000):
elseif ($points == 8000):
elseif ($points == 9000):
elseif ($points == 10000):
else:
  echo "???";
endif;
?>
  $(function(){
    $.jnotify('Rank Up Notification', '<p><?php echo $_SESSION['user']->name; ?> <?php echo "congrats you have earned another rank up";?></p><?php echo elgg_echo('vazco_karma:profile:rank'); ?> <?php echo $rank;?>' , '/updates/green_arrow_up.png', {lifeTime: 3000});
    });
</script>
  1. <script type="text/javascript">
  2. <?php
  3. $points = $karma->getUserPoints($user);
  4.     if ($points == 1000):
  5. elseif ($points == 2000):  
  6. elseif ($points == 3000):
  7. elseif ($points == 4000):
  8. elseif ($points == 5000):
  9. elseif ($points == 6000):
  10. elseif ($points == 7000):
  11. elseif ($points == 8000):
  12. elseif ($points == 9000):
  13. elseif ($points == 10000):
  14. else:
  15.   echo "???";
  16. endif;
  17. ?>
  18.   $(function(){
  19.     $.jnotify('Rank Up Notification', '<p><?php echo $_SESSION['user']->name; ?> <?php echo "congrats you have earned another rank up";?></p><?php echo elgg_echo('vazco_karma:profile:rank'); ?> <?php echo $rank;?>' , '/updates/green_arrow_up.png', {lifeTime: 3000});
  20.     });
  21. </script>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Your elseif syntax seems ok, but are considerd alternative: http://php.net/manual/en/control-struct ... syntax.php

More usual in PHP (at least from what I have seen) is the use of { and } to separate blocks of execution.

But if testing a bunch of numbers like that is your intention then you could use a switch instead of elseif structure:

http://php.net/manual/en/control-structures.switch.php

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 79 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
 
 

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