How to leave a negative number in php?

2

I have a rating of "A" and "B". If it's "A", I want the number to always be positive, if it's "B" I want it to always be negative.

I'm using a code in this logic:

if($classificacao==="A") 
    $sinal="abs";
elseif($classificacao==="B") 
    $sinal="-1*abs";

And the number is written in the form: $valor_final = $sinal($valor);

However, doing so causes the following error:

  

"Fatal error: Call to undefined function -1 * abs ()".

It recognizes how -1*abs is a non-existent function. Already in case of classification "A" is working. Could someone help me?

    
asked by anonymous 12.07.2017 / 14:35

2 answers

3

It seems to me to be a pretty clumsy solution and does not work just for the reason the error says: PHP will attempt to interpret the -1*abs value as the function name. Not only will the function not exist as it is an invalid name. You could use eval , but it gets as bad as it is completely unnecessary.

The simplest solution I see for your problem is to define only the multiplier within if :

if ($classificacao === "A") {
    $sinal = 1;
} else {
    $sinal = -1;
}
  

Note: If you have only two classifications, you do not need to check if the value is equal to B when it is different from A.

And display the result as follows:

$valor_final = $sinal * abs($valor);

Obviously there are many variations of this code that produce the same result. For example:

$valor_final = abs($valor);

if ($classificacao === "B") {
    $valor_final *= -1;
}

Given the existence of the C classification, as stated in the comments, which should retain the original value, something like:

function valueByClassification ($valor, $classificacao) {

    if ($classificacao === 'C') {

        $valor_final = $valor;

    } else {

        $valor_final = abs($valor);

        if ($classificacao === 'B') {
            $valor_final *= -1;
        }

    }

    return $valor_final;
}

I consider this form the best because you only use the abs function in the code once. In terms of performance it makes no difference, but in the matter of code maintenance it gets better, because if there is a need to change the called function, you need to do this in only one part of the code, not in several.

See the test of the function below:

$tests = [
    // {valor}        {classificacao}        {esperado}
    [    5,                  'A',                5     ],    
    [   -3,                  'A',                3     ],
    [    0,                  'A',                0     ],    

    [    5,                  'B',               -5     ],    
    [   -3,                  'B',               -3     ],    
    [    0,                  'B',                0     ],    

    [    5,                  'C',                5     ],    
    [   -3,                  'C',               -3     ],    
    [    0,                  'C',                0     ],    
];

foreach($tests as $i => $test) {
    list($valor, $classificacao, $esperado) = $test;

    $obtido = valueByClassification($valor, $classificacao);

    if ($obtido === $esperado) {
        echo "Teste $i foi aprovado.", PHP_EOL;
    } else {
        echo "Teste $i foi reprovado. Era esperado {$esperado}, mas foi obtido {$obtido}.", PHP_EOL;
    }
}
  

See working at Ideone .

The output generated is:

Teste 0 foi aprovado.
Teste 1 foi aprovado.
Teste 2 foi aprovado.
Teste 3 foi aprovado.
Teste 4 foi aprovado.
Teste 5 foi aprovado.
Teste 6 foi aprovado.
Teste 7 foi aprovado.
Teste 8 foi aprovado.
    
12.07.2017 / 14:50
0

I would re-solve your code with this function:

function myFunction($classificacao, $valor){
    if($classificacao==="A") 
        $valor_final = abs($valor);
    elseif($classificacao==="B") 
        $valor_final = abs($valor)*(-1);

    return $valor_final;
}
    
12.07.2017 / 14:50