简体   繁体   中英

PHP math greater than

I was busy making a level system for my site, I already have a if/else system that does it for ax amount of clicks.

So if you have clicked 44 times, you're level 1. The only problem is that people already clicked over 10.000 times(it's cool actually) but now I have to make a lot of ifs to create a level for it. This is what I already have:

  if($score['clicks'] >= 0 && $score['clicks'] <= 49)
  {
      $level = 'Level 2';
  }
  elseif($score['clicks'] >= 50 && $score['clicks'] <= 199)
  {
      $level = 'Level 3';
  }
  elseif($score['clicks'] >= 200 && $score['clicks'] <= 349)
  {
      $level = 'Level 4';
  }
  elseif($score['clicks'] >= 350 && $score['clicks'] <= 499)
  {
      $level = 'Level 5';
  }
  elseif($score['clicks'] >= 500 && $score['clicks'] <= 749)
  {
      $level = 'Level 6';
  }
  elseif($score['clicks'] >= 750 && $score['clicks'] <= 999)
  {
      $level = 'Level 7';
  }
  elseif($score['clicks'] >= 1000 && $score['clicks'] <= 1499)
  {
      $level = 'Level 8';
  }
  elseif($score['clicks'] >= 1500 && $score['clicks'] <= 1999)
  {
      $level = 'Level 9';
  }
  elseif($score['clicks'] >= 2000 && $score['clicks'] <= 2999)
  {
      $level = 'Level 10';
  }

I don't know how to make a if that can do it this way:

If $score['clicks'] is greater than 3000 then level is 11. And if $score['cliks'] is greater than 4000 then level is 12 etc...

Can you guys help me with this? Thank you and sorry for my bad English, it isn't my mother tongue.

Create a mathematical model instead of dozens of if statements. For example you could use: round(clicks^0.3) = level . That is a exponential function so users will level up fast in the beginning but then it get harder and harder to level up. You probably recognize this pattern from a lot of games.

In PHP code:

$level = round(pow($score['clicks'], 0.3));

A table of the result:

clicks^0.3  = level
-------------------
1^0.3       = 1
10^0.3      = 2
100^0.3     = 4
1000^0.3    = 8
10000^0.3   = 16

You can do something similar:

}elseif($score['clicks']>=3000){
  $level= 'Level ' . (floor(($score['clicks']-3000)/1000)+11);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM