PHP: $_POST in switch statement a right way? - php

I'm using switch statement on $_POST method and my script runs correctly but I think my code is not in a right way.
Here's my code:
<?php
switch(isset($_POST))
{
case isset($_POST['A']):
//do something
break;
case isset($_POST['B']):
//do something
break;
case isset($_POST['C']):
//do something
break;
}
?>
my script runs correctly but I think my switch statement is not GOOD enough. Is there more simplier way or BETTER way using switch statement and $_POST method?

It's horrible but I prefer this than if-else-evil-chain:
switch(true)
{
case isset($_POST['A']):
//do something
break;
case isset($_POST['B']):
//do something
break;
case isset($_POST['C']):
//do something
break;
}

check this out ;)
if (!empty($_POST)) {
switch ($_POST) {
case $_POST['A']:
#do something;
break;
case $_POST['B']:
#do something;
break;
case $_POST['C']:
#do something;
break;
}
}

Maybe you mean something similar?
<?php
if(isset($_POST['A'])){
// do something
}
if(isset($_POST['B'])){
// do something
}
if(isset($_POST['C'])){
// do something
}
?>
(This executes all the different matching branches instead of only the first one. Change the non-first if's to elseif if you want execute only the first matching branch.)

A nice way of putting a switch (){ case: } method is:
if (condition){
// Do Something
}elseif(condition){
// Do Something
}elseif(condition){
// Do Something
}
This chain, is horrible to look at, but something I would suggest using in your unique case, alternativley you could go with #lame-up-ducks answer, but this is what i'd personally recommend using

Related

A better PHP switch command

In pick basic there is a case command which functions similar to the switch command basically doing nested if then else commands. The code is like this:
begin case
case a=4;do something
case b=5 or c=6;do something
case y=x and f=z;do something
case 1;do something
end case
if any of the conditions are true, it falls into that case. I know PHP has the switch command, but that is limited to the value of one variable. Is there a way to code the above in PHP or javascript for that matter without a bunch of if then else commands similar to the above?
If you really don't want to use if/else, then switch (true) can work:
switch (true) {
case ($a === 4):
doSomething();
break;
case ($b === 5 || $c === 6):
doSomething();
break;
case ($y === $x || $f === $z):
doSomething();
break;
default:
break;
}
However, it is less typing, and it makes your code more readable, if you simply use if/else instead, just as Shomz suggested. I really wouldn't recommend using switch (true).
Replacing case with else if is only 3 bytes longer, I don't see the big deal because that is exactly what the if/else is for. Switches are used for single variables, as you said.
Your do something could also include a result variable or a flag that will be set if any of the conditions are met.
So this:
begin case
case a=4;do something
case b=5 or c=6;do something
case y=x and f=z;do something
case 1;do something
end case
could be:
var case = false;
if (a==4) {dosomething(); case = true}
else if (b==5 || c==6) {dosomething(); case = true}
else if (y==z || f==z) {dosomething(); case = true}
else if (1) {dosomething(); case = true} // supposedly the default case?

PHP a lot of else if another approach?

Here is my code I wanted to ask if there is a better way to do this? I'm going to need more "else if" options and I'm worried about performance
if($_GET['begin'] === 'yeg'){
} else if ($_GET['begin'] === 'deda') {
} else if ($_GET['begin'] === 'beara') {
} else if ($_GET['begin'] === 'eima') {
} else if ($_GET['begin'] === 'aba') {
}
You should use switch statement instead. A switch construct is more easily translated into a jump (or branch) table. This can make switch statements much more efficient than if-else when the case labels are close together. The idea is to place a bunch of jump instructions sequentially in memory and then add the value to the program counter. This replaces a sequence of comparison instructions with an add operation. - #Judge Maygarden
Why the switch statement and not if-else?
$begin = $_GET['begin'];
switch ($begin):
case 'yeg':
//do something
break;
case 'deda':
//do something
break;
case 'beara':
//do something
break;
case 'eima':
//do something
break;
case 'aba':
//do something
break;
endswitch;
You can try to use a switch statement instead:
switch ($_GET['begin']) {
case 'yeg':
break;
case 'deda':
break;
// Yada yada, continue like this as much as needed.
}

PHP switch statement with same value in multiple cases

I really like the structure of the switch statement compared to using multiple if else.
However some times I want to use the switch statement and have the same value in multiple cases. Can this be done somehow?
switch($fruit) {
case 'apple':
case 'orange':
// do something for both apples and oranges
break;
case: 'apple':
// do something for only apples
break;
case: 'orange':
// do something for only oranges
break;
}
I hope my example show what I intend to do...
No, it cannot. The first case that matches and everything following it until the first break statement or the end of the switch statement will be executed. If you break, you break out of the switch statement and cannot re-enter it. The best you could do is:
switch ($fruit) {
case 'apple':
case 'orange':
...
switch ($fruit) {
case 'apple':
...
case 'orange':
...
}
}
But really, don't. If you need some special action for those two before the individual switch, do an if (in_array($fruit, ['apple', 'orange'])) ... before the switch. Or rethink your entire program logic and structure to begin with.
Create some function and do it like this:
switch($fruit) {
case: 'apple':
apple_and_orange_function();
apple_function();
break;
case: 'orange':
apple_and_orange_function();
orange_function();
break;
}
You cannot match something multiple times, but you can write cascades like these:
switch($fruit) {
case 'apple':
// do something for only apples
case 'orange':
// do something for both apples and oranges
break;
case: 'grapefruit':
// do something for only grapefruits
break;
}
What you want can only be performed with if-else's or deceze's solution though

A multi-conditional switch statement?

This is something that I haven't seen in the PHPdoc for switch() so I'm not sure if it's possible, but I'd like to have a case which is multi-conditional, such as:
switch($this) {
case "yes" || "maybe":
include "filename.php";
break;
...
}
Is this valid syntax/is this even possible with a switch() statement?
Usually you'd just use case fall-through.
switch($this) {
case "yes":
case "maybe":
include "filename.php";
break;
...
}
Is this valid syntax/is this even possible with a switch() statement?
No and no. The expression will be evaluated as ("yes" or "maybe"), which will result in true. switch will then test against that result.
You want to use
case "yes":
case "maybe":
// some code
break;
Should be
switch($this) {
case "yes":
case "maybe":
include "filename.php";
break;
...
}
You can do this with fall-through:
switch ($this) {
case "yes":
case "no":
include "filename.php";
break;
}
Sure, just specify two cases without breaking the first one, like so:
switch($this) {
case "yes":
case "maybe":
include "filename.php";
break;
...
}
If you don't break a case, then any code for that case is run and continues on to execute additional code until the execution is broken. It will continue through all cases below it until it sees a break.

Multiple Separate Switch Statements

I have multiple switch statements in on one of the pages in order to pass different variables to the URL, as well as different case. I need these different switch statements because I need the different variables.
However, when I put a "default" in one of the switch statements, that default applies to every other switch statement and so when I use the variable of another switch statement in the URL, the default case of that other switch statement will appear on screen, along with the case of this switch statement.
All of my switch statements have one or more cases and I really cannot figure out how to get around this. Please may somebody help me?
Thanks in advance,
Calum.
This might be way off, but I think you need something like this:
if (isset($_POST['myvar'])) {
switch ($_POST['myvar'] {
case 1:
....
break;
default:
....
break;
}
} else if (isset($_POST['myvar2'])) {
switch ($_POST['myvar2'] {
case 1:
....
break;
default:
....
break;
}
}
Does that make sense?
make sure that you have a "break;" statement at the end of each of your cases, and that the default case is the last one. like this:
switch ($var) {
case 1: // do stuff 1;
break;
case 3: // do stuff 2;
break;
// ...
default: // do default stuff
}

Categories