0

I have to make a function to assign a string to a variable according to the value of other variable using OOP. I created this function (calculaIMC) inside a constructor class, and to achieve that I used an array ($imc_arr). I realize my code probably looks cumbersome and even inappropriate perhaps but it's an uni exercise designed to teach specific things. That's what I came up with so far:

<?php
class CalculoIndice{
    ...
    public $imc_arr = array(
        'Magreza grave',
        'Magreza moderada',
        'Magreza leve',
        'Saud&aacute;vel',
        'Sobrepeso',
        'Obesidade Grau I',
        'Obesidade Grau II (severa)',
        'Obesidade Grau III (m&oacute;rbida)');

    function CalculoIndice(){
        $this->preparaCalculo();
        $this->calculaIMC();
    }

    function preparaCalculo(){
        ...
    }

    function calculaIMC(){
        switch ($this->imc) {
            case ($this->imc < 16):
                $this->imc_cat = $this->imc_arr[0];
                break;
            case ($this->imc < 17):
                $this->imc_cat = $this->imc_arr[1];
                break;
            case ($this->imc < 18.5):
                $this->imc_cat = $this->imc_arr[2];
                break;
            case ($this->imc < 25):
                $this->imc_cat = $this->imc_arr[3];
                break;
            case ($this->imc < 30):
                $this->imc_cat = $this->imc_arr[4];
                break;
            case ($this->imc < 35):
                $this->imc_cat = $this->imc_arr[5];
                break;
            case ($this->imc < 40):
                $this->imc_cat = $this->imc_arr[6];
                break;
            default:
                $this->imc_cat = $this->imc_arr[7];
        }
    }
}
?>

It is not working. I couldn't quite figure what is going on and what the problem is but it doesn't echo the variable (imc_cat) as I intended. I'm sure it's something simple that I am missing but I've spent a few hours looking for an answer with no success. I appreciate any insight as to what may be wrong.

1
  • First, your switch statement looks incorrect because all will equate to true. You should use rage i.e $this->imc >0 && $this->imc < 16 , $this->imc >16 && $this->imc < 18.5, $this->imc >18.5 && $this->imc < 25 Commented Oct 15, 2017 at 2:36

1 Answer 1

2

You don't put conditions inside case expressions. case performs an equality test with the expression in the switch() statement, so

case ($this->imc < 16):

means

if ($this->imc == ($this->imc < 16))

You should be using if/elseif instead of switch/case.

if ($this->imc < 16) {
    $this->imc_cat = $this->imc_arr[0];
} elseif ($this->imc < 17) {
    $this->imc_cat = $this->imc_arr[1];
} ...
} else {
    $this->imc_cat = $this->imc_arr[7];
}

Actually, there's a method that some programmers use (but I don't personally endorse):

switch(true) {
    case ($this->imc < 16):
        $this->imc_cat = $this->imc_arr[0];
        break;
    case ($this->imc < 17):
        $this->imc_cat = $this->imc_arr[1];
        break;
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That solved my problem. As I thought my mistake was a simple one, in this case not understanding properly how 'switch' works. I had seen the "true" method during my researches but it wasn't working, probably because of something else, then I completely forgot about it. It's now working. The if/else solution may be a better one, but considering it's just an exercise for university I will leave as it is. Thank you very much again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.