Comparing strings to switch in PHP

Asked

Viewed 60 times

0

The point is this:

A company will give a salary increase to its employees from according to the category of each employee. The increase will follow the following rule:

  • Employees in categories A and H will earn 10% increase over salary;

  • Employees in categories B, D and E will earn 15% increase over salary;

  • Employees in categories K and R will earn 25% increase over salary;

  • S category employees will earn 35% increase over salary;

  • Employees in categories X and Z will earn 50% increase over salary.

  • Employees in other categories earn 5% increase over salary.

Using the "Switch" command, write a PHP algorithm to receive current category and salary, then display the salary readjusted from an employee to example.

I made this code:

<?php

$salario = 1045;
$salarioFinal = 0;
$percentagem = 0;

$categoria = 'A';

switch ($categoria) {
    case 'A' or 'H':
        $percentagem = $salario * 10 / 100;
        $salarioFinal = $salario + $percentagem;
        echo "Salário atual: $salario <br/>";
        echo "Salário final: R$ $salarioFinal";
        break;

    case 'B' or 'D' or 'E';
        $percentagem = $salario * 15 / 100;
        $salarioFinal = $salario + $percentagem;
        echo "Salário atual: $salario <br/>";
        echo "Salário final: R$ $salarioFinal";
        break;

    case 'K' or 'R';
        $percentagem = $salario * 25 / 100;
        $salarioFinal = $salario + $percentagem;
        echo "Salário atual: $salario <br/>";
        echo "Salário final: R$ $salarioFinal";
        break;

    case 'X' or 'Z';
        $percentagem = $salario * 50 / 100;
        $salarioFinal = $salario + $percentagem;
        echo "Salário atual: $salario <br/>";
        echo "Salário final: R$ $salarioFinal";
        break;

    default:
        $percentagem = $salario * 5 / 100;
        $salarioFinal = $salario + $percentagem;
        echo "Salário atual: $salario <br/>";
        echo "Salário final: R$ $salarioFinal";
        break;
}

?>

The problem is that it always jumps to the default condition by returning:

Salário atual: 1045
Salário final: R$ 1149.5

that is, is not comparing the string according to the variable $categoria = 'A'; declared at the beginning of the code.

1 answer

3


Make no mistake (Initially I thought I gave so I answered, in fact the code does exactly what I should do, I did not know that PHP allowed the syntax used). I did it in a way that is correct and much more simplified avoiding code repetition and after observing that there is no error, the answer is only due to simplification.

The whole switch can be exchanged for 1 line, but I did not change to avoid the request.

<?php
$salario = 1045;
$categoria = 'A';
switch ($categoria) {
    case 'A':
    case 'H':
        $percentagem = 10;
        break;
    case 'B':
    case 'D':
    case 'E';
        $percentagem = 15;
        break;
    case 'K':
    case 'R';
        $percentagem = 25;
        break;
    case 'X':
    case 'Z';
        $percentagem = 50;
        break;
    default:
        $percentagem = 5;
        break;
}
$salarioFinal = $salario + $salario * $porcentagem / 100;
echo "Salário atual: $salario <br/>";
echo "Salário final: R$ $salarioFinal";

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Understand that the break is just to say that ended the action selected in switch. If you don’t have one it keeps running what is below, then it can accept multiple case with the same action. You can even make a combination of having an equal part for several, but having a part in each case individual.

I haven’t changed but the ideal would be to be more organized and progress all forward or backward, one of them (the default - 5) broke the logical sequence and becomes less readable. Part of the blame is the statement.

Browser other questions tagged

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