Calculation in php giving error

Asked

Viewed 247 times

2

On my site I have the following code to calculate discounts:

$valor_a_ser_calculado =$_POST['loc']; // valor original
$quantos_porcento = 75/100; //isso equivale a 7% 
$loc = $quantos_porcento * $valor_a_ser_calculado . "\n";

only the following mistake is making:

PHP Warning:  A non-numeric value encountered in /home/jp/public_html/orcamento.php on line 6

I’d like to know how to fix it

  • $loc = ($quantos_porcento * $valor_a_ser_calculado ). "\n";

  • Separate the numeric part of the string part with parentheses. Also, check that the POST value is correct first of all.

  • I put the relatives and gave error, and yes the value of the post is correct.

  • The one where I have to put the relatives exactly?

1 answer

3

You have two immediate problems in the code: You are not checking whether the POST value is correct, and mixing multiplication with concatenation. Test with this:

$valorasercalculado = $_POST['loc']; // valor original
echo "Valor original: $valorasercalculado <br>";

$quantos_porcento = 75/100; //isso equivale a 7.5% 
echo "Porcentagem: $quantosporcento <br>";

$loc = $quantos_porcento * $valor_a_ser_calculado;
echo "Resultado: $loc <br>";

In this case, you will only have warnings if any of the values are not numerical.

The ideal would be to rethink something along these lines:

$valorasercalculado = isset($_POST['loc']) ? $_POST['loc'] : 0;

Or even

if (!isset($_POST['loc'])) die 'Valor não fornecido';

right at the beginning of the code.

As well remembered by fellow @Rafaelsalomao, PHP has a specific function to test if a value is numerical. Joining the two ideas:

if (!isset($_POST['loc'])) die 'Valor não fornecido';
if (!is_numeric($_POST['loc'])) die 'O valor precisa ser numérico';

(remembering that ! before the function means "not", in the above cases, if the value is NOT set, the script stops. If the value is NOT numerical, the script also stops).

Handbook:

http://php.net/manual/en/function.isset.php

http://php.net/manual/en/function.is-numeric.php

  • Bacco the merit of the answer is yours! I just suggested an edition using php’s is_numeric function to validate the value sent by POST. Remembering that you should also validate this condition in your form.

  • Bacco thank you very much worked perfectly, thanks a lot.

  • @Rafaelsalomão thank you for the intention, feel free to propose an alternative answer (multiple responses are welcome on the site, provided that complementary). Initially my intention was just to have the author check the parameters.

  • @Joãopedroalmeida I will improve a little the answer, worth noting that colleague Rafael Solomon suggested the use of is_numeric for you to see if the past value is really a number.

  • @Rafaelsalomão added his suggestion in another way, grateful.

  • Okay, Brother, it’s luxury!

  • @Rafaelsalomão but if you want to complement with another answer, you are welcome!

  • @Rafaelsalomão was worth man, the two answers were good but the Stock worked better for my case.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.