Switch statement

Hey all

I am trying to understand how to use switch and case instead of if and else.

I have this code that works


if (empty ($records))
       {
        	include 'searchform.html.php';
	        echo htmlspecialchars('No Records Found', ENT_QUOTES, 'utf-8');
	        exit();
        }
else
	{
		include 'files.html.php';
	}

I am trying to duplicate it using a switch statement…here’s the code that is not working…


switch($records)
	{
		case empty:
		        include 'searchform.html.php';
		        echo htmlspecialchars('No Records Found', ENT_QUOTES, 'utf-8');
		exit();							
		break;
		default:
			include 'files.html.php';
		break;
	}

I get this error… Parse error: parse error, expecting `‘(’’ in C:\wamp\www\ est\maintain.php on line 129. WHich in this case matches up with the ‘case empty:’ line

I have two questions…

First how do I change the above code so that it works like the if else statement?

and…

Does anyone know some good resources that can I can use to get a better handle on the syntax?

What type of things would you use the switch statement for?

When you are testing a variable against a number of different conditions.


switch($hour)
    {
        case < 12 :
                 echo 'good morning!' ;
        break;
        case < 18 :
                 echo 'good afternoon!' ;
        break;
        case < 21 :
                 echo 'good evening!' ;
        break;
        default:
            echo 'goodnight!'
        break;
    }

*untested, just to give a simple example

As Rubble said, your example only had 2 outcomes so was not a good candidate.


switch(count($records)) {
        case 0:
                include 'searchform.html.php';
                echo htmlspecialchars('No Records Found', ENT_QUOTES, 'utf-8');
                exit();                            

        default:
            include 'files.html.php';

}

Also, you don’t need to break after exit since the script will stop running once exit is called. Similar to the default break isn’t needed because the end of the conditional has been reached.

This is part of a script that I am using for another project and it won’t actually be used. I am trying to get a better understanding of how to use the switch statement that’s all.

You mentioned that the if statement was a better choice. What type of things would you use the switch statement for?

I am not understanding why you want to use the switch statement here. The if else syntax makes more sense to me in this instance.

empty is a function. Each case in the switch statement expects a value to test against $records, so when it encounters a function vs. A value, the parse error is generated.

You might want keep your test for empty in an if statement, then put your switch statement in the else condition to test for the various values of $records knowing that it is not empty at this point.

Hi, use quotes in case , like
switch($records)
{
case “empty”:
include ‘searchform.html.php’;
echo htmlspecialchars(‘No Records Found’, ENT_QUOTES, ‘utf-8’);
exit();
break;
default:
include ‘files.html.php’;
break;
}