0

I wanted to check if my switch statement is true and if it is, I want to do something with it. e.g.

$a = trim($_POST['code']);

    switch ($a) {
    case "123";
        break;
    case "467";
        break;
    default : "incorrect";
    }

I then want to do an if statement like this

if(switch == true) {
    $a = trim($_POST['code']);
}
else {
$Error ="incorrect code";
$hasError = true;
}

if(switch == true) is not right. How will I check if the switch statement is true?

Thanks for your time.

2
  • a lot of answers and edits, but I still can't really gather what you want to do. What exactly do you want to test? if $_POST['code'] is set at all? or if one of the case 'xy': conditions match?
    – cypherabe
    Commented Mar 3, 2014 at 10:43
  • php.net/in_array
    – hakre
    Commented Mar 3, 2014 at 11:05

6 Answers 6

4

A switch statement is not an expression and thus cannot be "true". You can only use a switch statement for side-effects, such as assigning a variable.

For instance (fixing the syntax errors)

$valid = false; # default to false

switch ($a) {
    case "123":
    case "467":
        $valid = true; # yay, "valid!"
        break;
    default:
        break;
}

if ($valid === true) {
  # ..
}

Although, in practice, I would rarely write code that looked like that - just do the action in the switch or use the if-statement directly; use a function, perhaps containing the switch-statement to write cleaner code.

function isValid($secretCode) {
  switch ($secretCode) {
    case "123":
    case "467":
      return true;
    default:
      return false;
  }
}

if (isValid($a)) {
  # ..
}
9
  • just wondering where you got the variable $secretCode or is that meant to be $a? Commented Mar 3, 2014 at 10:50
  • @user3057514 $a is the variable outside the function (e.g. $a = trim($_POST['code'])), $secretCode is the parameter in the function, and initially has the same value of $a which was supplied as an argument (e.g. isValid($a)). I changed the parameter name to make the difference in scopes/variables more apparent, but you could call it something else (including $a). Commented Mar 3, 2014 at 10:57
  • thanks very much didnt look at it that way, also in your function i didnt see you break any of the cases. how will the switch stop when it find the correct case code? I was taught to always break Commented Mar 3, 2014 at 11:01
  • @user3057514 Since return will exit the function (and thus switch statement) immediately, there is no reason to also include a break. Commented Mar 3, 2014 at 11:02
  • @user2864740: As there is no need to break, there is also no need for the default case, just end the function with a return false;. The other function that you might have been probably looking for is called in_array, it also returns TRUE/FALSE, valid values can be passed as array.
    – hakre
    Commented Mar 3, 2014 at 11:05
2

The case keyword ends with a :(colon) and not with a ; (semicolon)

2
  • 4
    @ShankarDamodaran I'd guess because, while you're correct that the syntax is incorrect, this doesn't address the whole question as asked. Commented Mar 3, 2014 at 10:37
  • @ShankarDamodaran look here please switch i still doing it with the ; but thanks for the heads up Commented Mar 3, 2014 at 10:46
1

I'd go in a complete opposite direction and use in_array instead:

//If trim($_POST['code']) is either 123 or 467 then..
if ( in_array( trim( $_POST['code'] ), array("123", "467") ) {
    echo 'Correct!';
} else {
    echo 'Incorrect!';
}
1
  • The only real answer!!
    – Hardy
    Commented Mar 3, 2014 at 10:39
1
$a = trim($_POST['code']);
$hasError = 0;

switch ($a) {
    case "123":
    break;

    case "467":
    break;

    default : 
        $hasError = 1;
    break;
}
1
  • why don't you ask OP ? or flame the other answers as they are similar ?
    – Kalzem
    Commented Mar 3, 2014 at 10:37
0

Your code should be like this :

    $a = trim($_POST['code']);

    switch ($a) {
    case "123":
        break;
    case "467":
        break;
    default : "incorrect";
    }

replace : (colon) with ; (semicolon).

0
$switch_true = true;
switch($a){
    case 123: break; // $switch_true stays true
    case 267: break; // $switch_true stays true
    // for any other value
    default: $switch_true = false; break; // $switch_true becomes false
}
var_dump($switch_true);

More about switch here.

But you should use:

$switch_true = in_array($a, array(123, 267));

Switch is not really meant for this kind of use.

2
  • 1
    In your answer, I think it should be default: $switch_true = false;
    – RaviRokkam
    Commented Mar 3, 2014 at 10:47
  • @RaviRokkam +1. I haven't waken up yet. :)
    – CodeAngry
    Commented Mar 3, 2014 at 10:50

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