Skip to content
Advertisement

PHP Variable value based on user input

I’m building an application to help customer calculate various product prices.

Right now I’m building a feature where user enters a single number to the application and submits a form. Based on that number, I would like to define another variables value.

What I’d like to achieve

If user input is number between 1-10, set variable number to 200.

If user input is number between 11-20, set variable number to 400.

If user input is number between 21-30, set variable number to 600.

If user input is number between 31-40, set variable number to 800.

If user input is number between 41-50, set variable number to 1000.

And so on… So basically increasing by 200 every tenth. Of course, I could do something like this:

JavaScript

But it isn’t really a great solution, because it takes lot of code and if user sets number out of the determined range it doesn’t work..

How can I implement this with as little amount of code as possible?

Advertisement

Answer

If I have the math right, you just need to divide the number by 10, and use ceil to round the fraction up. From there, multiply it by 200;

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement