Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit a PHP variable (number) to 100

Tags:

php

I have a PHP function which returns a number, and I want to display it as a percentage from 1 - 100. Anything over 100 should just be reevaluated to 100. How can I do that?

$myvar = 120;
$my_percentage = <$myvar, max value is 100>

I'm using Laravel, if that helps. Thanks!

like image 398
Leon Avatar asked Jun 20 '26 15:06

Leon


2 Answers

This is the simplest way for restricting the number

$my_percentage = min(100, $myvar); 
like image 153
Muhammad Ali Avatar answered Jun 22 '26 07:06

Muhammad Ali


If you want to ensure the value is always less than or equal to 100, then take the minimum (using min()) of the value and 100...

$my_percentage = min(100, $myvar);
like image 23
Nigel Ren Avatar answered Jun 22 '26 06:06

Nigel Ren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!