It is common when you first encounter this doubt.
I have to perform an operation that is dependent on a condition:
Example 1
$operacao = 'somar';
$valor1 = 5;
$valor2 = 10;
$saldo = null;
if($operacao == 'somar'){
$saldo = $valor1 + $valor2;
}else{
$saldo = $valor1 - $valor2;
}
if($saldo != null){
echo $saldo;
}
Example 2
$operacao = 'somar';
$valor1 = 5;
$valor2 = 10;
$saldo = null;
$valor2 *= (($operacao == 'somar') ? 1 : -1);
$saldo = $valor1 + $valor2;
if($saldo != null){
echo $saldo;
}
Question
What is the best way to accomplish this task, so that it does not occupy many lines, not much memory?