Home > Software design >  PHP, switch case returned undefined variable
PHP, switch case returned undefined variable

Time:05-17

Hy,

I got switch case inside function when but when i call it, i got error Undefined Variable and i don't know why (i use PHP 8)

private function getIDFromBrand() {
    switch ($this->brand) {
        case "Niky":
            $id = 1;
            break;
        case "Pumo":
            $id = 4;
            break;
        case "Coke":
            if ($this->typecoke== 0) {
                $id = 2;
            } else {
                if ($this->typecoke== 1) {
                    $id = 3;
                }
            }
            break;
        case "Tomato":
            $id = 5;
            break;
        case "Riles":
            $id = 6;
            break;
        case "TEST":
            $id = 7;
            break;
    }

    return $id; // Error Undefined variable $id
}

When i declare $id at the top of my function, like

$id = null

or

$id = 0

The switch doesn't update it, so it will return null or 0, it will return the declared value.

CodePudding user response:

Your switch statement has no default branch, so if $this->brand is, say, "Stack Overflow", it will not run any of the statements, and $id will never be set.

See the PHP manual for the switch statement:

A special case is the default case. This case matches anything that wasn't matched by the other cases.

Similarly, if $this->brand is "Coke", but $this->typecoke is, say, 42, it will not match either of the conditions in that branch.

switch ($this->brand) {
    case "Niky":
        $id = 1;
        break;
    case "Pumo":
        $id = 4;
        break;
    case "Coke":
        if ($this->typecoke== 0) {
            $id = 2;
        } elseif ($this->typecoke== 1) {
            $id = 3;
        } else {
            $id = -1; // WAS PREVIOUSLY NOT SET
        }
        break;
    case "Tomato":
        $id = 5;
        break;
    case "Riles":
        $id = 6;
        break;
    case "TEST":
        $id = 7;
        break;
    default:
        $id = -1; // WAS PREVIOUSLY NOT SET
        break;
}

CodePudding user response:

I guess you have defined the $id variable outside of the class. So, basically in PHP, we cannot update or redefine a variable that is global variable, inside of a function. So, to redefine that variable, add this line to the code:

global $id;

Example:

    private function getIDFromBrand() {
        global $id;
        switch ($this->brand) {
            case "Niky":
                $id = 1;
                break;
            case "Pumo":
                $id = 4;
                break;
            case "Coke":
                if ($this->typecoke== 0) {
                    $id = 2;
                } else {
                    if ($this->typecoke== 1) {
                        $id = 3;
                    }
                }
                break;
            case "Tomato":
                $id = 5;
                break;
            case "Riles":
                $id = 6;
                break;
            case "TEST":
                $id = 7;
                break;
        }
    
        return $id; // Error Undefined variable $id
    }

Hope it helps.

  • Related