Question :
I have the variable $peso
that oscillates between 1000 and 50000 (grams)
There is a freight table that I have as
1000a2000 => 10,00
2000a3000 => 20,00
…
9000a10000 => 100,00
kg_adicional => 2,25
I have ifs up to 10000 (grams), above that I should calculate the additional value, for example
$peso = 18000
I have to get the value of 9000a10000 ($ 100) and add another 8 KG, but I do not know how to break this variable and do the validation.
Answer :
If the quantities are proportional, you do not even have to use ifs :
1000a2000 => 10,00
2000a3000 => 20,00
3000a4000 => 30,00
4000a5000 => 40,00
5000a6000 => 50,00
6000a7000 => 60,00
7000a8000 => 70,00
8000a9000 => 80,00
9000a10000 => 90,00 (aqui é 90 ou 100?)
In theory, the following formula already works.
$peso_excedente = $peso - 10000;
$peso_nominal = $peso - $peso_excedente;
$total = (floor($peso_nominal / 1000) * 10) + (floor($peso_excedente / 1000) * 2.25);