This should be a good beginner tutorial, especially if you have used some functions and if statements and haven’t gone much further. I will explain the concept as we go along, first let’s review the Ternary Operator / Operators that we will use.

$x = $y < 0 ? -1 : 1;

That is sort of a quick “what is the sign” test for signed numbers. If means if y is less than 0 x is negative 1, otherwise it is positive 1. 0 is considered +1 in that case. Following so far? You just need to understand the basic rules so you can understand what is happening in a complex line of code.

$x = $y == 0 ? 0 : ($y < 0 ? -1 : 1);

In that case it mean if y is 0 x is 0, otherwise x is either -1 or +1. Let’s use that principal to make a function that accepts 3 arguments and tells us whether argument 1 is within the bounds of arguments 2 and 3.

function custom_clamp_check($number, $low, $high){
return $number > $high ? false : ($number < $low ? false : true);
}

Think about that function like this, if the first statment is true, we return false, if it isn’t we check the second statment. The function returns false unless the $number is between $high and $low or equal to either. This is intentionally written to work well with floats and integers. Other variable types should not be used.

PHP has built in min and max functions like in this example.

$x = min($y, 0);
$x = max(5, 12);

That just gives you the higher number of the two numbers put into the function. Actually you can do max(1, 4, 99, -5) since it allows more arguments, or max(array(1, 4, 99, -5)) with the same result. It returns the highest of whatever is put in. I don’t know if min/max have many pros or cons over statements like we used earlier, but some users have said min/max are slower and less efficient. Regardless of that this article is focusing on using operators in statements to construct the desired functions, even if only for learning purposes.

Our final function is a clamp function, it clamps the value entered to be equal to $high, equal to $low, or between the two.

function custom_clamp($number, $low, $high){
return $number > $high ? $high : ($number < $low ? $low : $number);
}

You should now understand how the function works and what the results will be. custom_clamp(1.0115, 1.0, 0.0) would give you 1.0, the exact high value you put in. These functions are useful for many things, and as I described earlier we can use integers or floats. If a user entered a comment we could check if it’s length is within the range we want.

if(custom_clamp_check(strlen($_GET['comment']), 20, 512) == false){
return "Your comment must be 20 to 512 chars in length.";
}

If the user must also enter a number within 1 and 10 and we want to make sure there are no other numbers possible, we could force it with the clamp function.

$entered_number = custom_clamp(intval($_GET['number']), 1, 10);

I added intval to make this even more strict, since users could have entered 1.0002 and we probably only want whole numbers. The final use shows how useful the function can be in math. The goal is to keep the product above 0 since dividing by zero is not acceptable.

$angle = 1.0 / custom_clamp($radius * pi(), 0.0000001, 1.0);