0

earlier today i asked this question regarding retuning the output of a switch statement.

the conclusion was that return in a switch statement should be used only inside a function.

now i am trying to use the return in a Method in a Class. this is the code:

class ClassName{

     public function www($foo)
     {
        switch ($foo) {       
            case 1:
                return '1';
                break;

            case 2:
                return '2';
                break;

            default:
                return 'no matching values were sent to the function';
                break;
        }
    }
}

$foo = 2;
ClassName::www($foo);

when i replace the return with echo i get an output. why it dosnt work with return?

1
  • 1
    Along with the answer, you don't need those breaks since return exits the switch and function. Mar 2, 2015 at 19:04

1 Answer 1

4

return literally returns the value to the calling code, you have to actually do something with it:

echo ClassName::www($foo);
//or
$val = ClassName::www($foo);
echo $val * 4;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.