33 C
Pakistan
Wednesday, May 29, 2024

PHP max() function

To determine the highest value in a list of items, utilize PHP’s max() function. Here are some illustrations of how this functions:

echo max(2, 3, 1, 6, 7);  // 7
echo max([2, 4, 5]); // 5

I discovered a piece of code I had written and quickly refactored it using this function. Watch the video below to see it in action, or read the full article below.

Examining the Authentic Code

We used a Ternary Operator to handle this requirement in our current setup. Assume that a SaaS application typically offers Bronze, Silver, and Gold plans as its pricing tiers. We guarantee that purchasers of the Gold Plan purchase a minimum of ten licenses.

if ($selectedTier === 'gold') {
    $licenses = $licenses > 10 ? $licenses : 10;
}

The Ternary operator’s condition verifies whether the Gold Plan is chosen. If this is the case, it assesses the inner Ternary operator, which compares 10 to the variable $licenses. $licenses keeps its current value unless it is less than 10, in which case it is replaced with 10. The $licenses are left untouched if the Gold plan is not chosen.

This method works, but the code is complicated and not very easy to read. I was wondering: “Can I simplify this to make it more readable and elegant?”

PHP Max Function Utilized for Refactoring
That’s where the useful PHP Max function enters the picture! To determine the maximum value among the supplied integers, utilize PHP’s max function, a mathematical function.

It looked like the answer to our previous question was “Yes,” so let’s see how we can rephrase it:

if ($selectedTier === 'gold') {
    $licenses = max($licenses, 10);
}

In this simplified version, the max function compares the number of selected licenses with ten and returns the greater value if the Gold Plan is chosen. The $licenses are unaffected if the Gold Plan is not chosen.

Not only is this new code snippet shorter, but it is also simpler to comprehend. If you know how to use the max function, you’ll understand how this code works right away. One important feature of clean and maintainable code is that this version is much easier to read than the previous one.

Fun Fact: When simplifying complex Ternary Operators involving numerical comparisons, PHP’s max function can be a very useful tool.

Review

Using PHP’s max function, this small-scale but effective code refactor emphasizes how important it is to constantly strive for elegance and simplification in your codebase. It is simpler to read, maintain, and distribute an elegant codebase among team members.

The max function may be one of the PHP functions that can help you code more quickly, whether you’re developing a SaaS application or any other application that needs conditional control flow based on numerical values. So why not use the handy built-in max function instead of that intricate Ternary Operator?

Always keep in mind that simpler code is easier to read later on.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles