RezaM
RezaM

Reputation: 167

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 a x 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.

Upvotes: 0

Views: 82

Answers (2)

Sawny
Sawny

Reputation: 1423

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

Upvotes: 4

Lajos Veres
Lajos Veres

Reputation: 13725

You can do something similar:

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

Upvotes: 2

Related Questions