Norman
Norman

Reputation: 6365

Using switch inside a function, should break be used after a return true?

function newLinks($a,$b,$c,$d,$e,&$links) {

    switch ($a) {
        case '00':
        $links = "Under Process";
        return true;
           break;


        case '01':
        $links = "Processed";
        return true;
            break;

        case '10':
        $links = "Deleted. Under Process";
        return true;
           break;

        case '11':
        $links = "Deleted. Processed";
            return true;
            break;

        default:
        return false;
    }
}

Is break needed after return true?

Because I do something like this:

if ( newLinks($a,$b,$c,$d,$e,$links) ) {
    echo $links;
} else {
    echo 'Failed';
}

Upvotes: 2

Views: 398

Answers (5)

user2568509
user2568509

Reputation:

Just like some pals said, break isn't need btw

Upvotes: 1

Aris
Aris

Reputation: 5055

well you can leave the breaks for clarity to separate the cases. However this can cause bigger issues in the future, because you may not notice the returns.

Why not set a variable in each case, and return once after the switch function is over? In my opinion this will make the code much clearer.

Upvotes: 2

felipe.zkn
felipe.zkn

Reputation: 2060

The return statement will just pull it off the function. You don't need the break statement in this case.

Upvotes: 1

Michael Kunst
Michael Kunst

Reputation: 2988

No, you won't need the break;, as the function is not executing further after a return call.

Upvotes: 2

John Conde
John Conde

Reputation: 219864

Break is unecessary in this case as the function immediately terminates after returning the value so the break statement would never be reached anyway.

Upvotes: 2

Related Questions