Jump to content

Recommended Posts

I couldn't understand switch statements in .net and I still don't get them with php. Can someone turn this into a switch statement, I just can't wrap my head around then.

if (isset($_POST['news'])){
add($_POST['text']);
header("location: admin.php");

} elseif (isset($_POST['rnews'])){
deletenews($_POST['number']);
header("location: admin.php");

} elseif (isset($_POST['taddm'])){
taddmember($_POST['website'], $_POST['name']);
header("location: admin.php");

} elseif (isset($_POST['tdelm'])){
tdeletemember($_POST['number']);
header("location: admin.php");

} elseif (isset($_POST['addm'])){
addmember($_POST['website'], $_POST['name']);
header("location: admin.php");

} elseif (isset($_POST['delm'])){
deletemember($_POST['number']);
header("location: admin.php");

} elseif (isset($_POST['tnews'])){
tadd($_POST['text']);
 header("location: admin.php");

} elseif (isset($_POST['trnews'])){
tdelete($_POST['number']);
header("location: admin.php");

} elseif (isset($_POST['video'])){
videoupdate($_POST['link']);
header("location: admin.php");

} else {
echo " Failed";
}

Link to comment
https://forums.phpfreaks.com/topic/224790-switch-statement-bugger-me/
Share on other sites

Switches are like a limited, inflexible version of if/then/else.  They only work with one variable, and you have to list the exact values that variable can take.

 

Let's say you use $_POST['mode'] to tell your script what mode to run in.  Then you can do like this:

 

switch($_POST['mode']) {
  case 'register':
    # Do register stuff
    break;
  case 'login':
    # Do login stuff
    break;
  default:
    # Handle unrecognized case
}

 

The advantage of a switch is that it enforces a good coding practice - it can reduce complexity of your code by forcing you to deal with a single variable with clearly defined values.  You can't cheat with a switch because php won't allow it.

Your code could also be put in a switch, you would just need to test for the bool true. (I wouldn't generally recommend this though).

 

switch (true) {
  case isset($_POST['news']):
    add($_POST['text']);
    header("location: admin.php");
    break;
  case isset($_POST['rnews']):
    deletenews($_POST['number']);
    header("location: admin.php");
    break;
  // etc etc
}

I definitely wouldn't recommend what's in thorpe's last post there :)  That's not how switch was intended to be used.  But apparently it does work.  If you do use it, please document it to save the sanity of other people who see your code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.