Q:

How to figure out a users earnt exp based upon calculating the difference by user and player by x?

How would I figure out a users earnt EXP based upon calculating the difference by user and player by x using this format?
If the user is 60 levels less than me, I would get 0 exp (as that's just bullying).
If the user is within 21 and 59 levels less than me, I would get 1 exp.
If the user is less than 20 levels of mine, I would get 2 exp.
If the user is the equal level they would get 3 exp, however if the user is within my level, and 20 above me I would get 4 exp.
If they were between 21 and 40 levels above me I would get 5 exp
If they were 41 and 60 levels above me I would get get 6 exp
and if they were 61+ levels above me I would get 7 exp
So I'm looking to do something like this -
How would it structure if I wanted to make it (player being me, and the user is the opponent)
if( $PLAYER->level is more than 60 of $USERS->level )
{
$earn = 0;
} 
elseif( $PLAYER->level is between 21 and 59 more than $USERS->level )
{
$earn = 1;
}
elseif( $PLAYER->level is between 1 and 20 more than $USERS->level )
{
$earn = 2;
}
elseif( $PLAYER->level == $USERS->level )
{
$earn = 3;
}

I know in SQL they have a between operator, but I cannot seem to find a a PHP one and/or figure out a solution to display what I intend to do above.
Is it possible to do what I intend? Or do I need to find a new solution?

A:

It looks like you might be better off with some formula:
// keep the sign separate
int sign = enemyLevel > playerLevel ? 1 : -1;
// Calculate the xp as 1 xp per 20 level differences
// Ensures that no negative xp is given
int givenExp = math.min(0, 3 + (sign * math.floor(math.abs(enemyLevel - playerLevel)/20.f)));

What this does is clamp each 20 levels to 1 xp. Everything is rounded down, so 39 levels would be the same as 20 levels, but 40 levels would give another xp. 
To make things simple, I removed the sign and added it later, but you could handle negative and positive level differences separately if you want. At least to me, it seems like taking down a mob 20 levels higher than you would be a feat worth more xp.

