Switch Statements in PHP

PizzaHut

Baseband Member
Messages
22
Location
Australia
I am mucking around with PHP and MySqli trying to create a register form.

Usually you can check if the fields have not been field out by doing something like:

Code:
if(!isset($_POST[???])) {
  - Warning or what ever -
}

What I am trying to do is create a switch statement, in the actual switch I want the cases to be $username, $password, $email etc.

I vision it something like this:

Code:
switch(!isset) {
    case $username:
       echo "Please fill out....";
    break;

   ---- repeat for other fields ----
}

The problem is that !isset is completely useless in this case unless i do something like !isset(VARIABLE) and then make the cases = the variable but I do not know how to do that.

Sorry if I explained it bad but it is confusing me aswell XD
 
Well that's what a switch statement is meant for, checking 1 variable that can have several values and doing conditional logic based on those separate cases.

If you do "case $username" etc. for each field, it's only going to hit once unless you run a loop on that switch for each field (I'm assuming you're checking the values for validation?). An If statement is still going to be the best solution IMO.
 
Hi,
i think in first you'ld should ask if your variable ($username) is empty. That's the job of the isset function. You make it in a if loop and if your variable is not NULL, than its enter in the switch loop. Or "Please fill out..." will appear. You'll make it with all the variable.
Ex : If(isset($_POST['username']{
$username = $_POST['username'];
Switch($username){
case"(username's text)":
echo "thank you";}
}else{
echo "Please fill out...";}

watch out you should control if your varibale is really empty with a empty function.
I hope that help you....
 
Back
Top Bottom