Continue case in switch - php

I have following code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
break;
}
case (something3):
{
break;
}
}
Also the switch statement must check what one of cases give a TRUE, that is not the problem, the problem is, that i have now a case, where inside of case ... break; after checking other data, i wish to choose other switch-case, the one, that following him.
I have try do this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check)
{
something3 = true;
continue;
}
break;
}
case (something3):
{
break;
}
}
But PHP do not wish to go in to the case (something3): its break the full switch statement. How i can pass the rest of code of one case and jump to the next?

This is what's called a "fall-through". Try organizing your code with this concept.
switch (foo) {
// fall case 1 through 2
case 1:
case 2:
// something runs for case 1 and 2
break;
case 3:
// something runs for case 3
break;
}

Using your code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check) {
something3 = true;
}
else {
break;
}
}
case (something3):
{
break;
}
}
This will get to case something2 and run your check. If your check passes then it doesn't run the break statement which allows the switch to "fall through" and also do something3.

case (something2):
{
if(!check)
{
break;
}
}

Try this:
switch(true) {
case (something):
{
break;
{
case (something2):
{
if (!check) {
break;
}
}
case (something3):
{
break;
}
}

I had some similar situation and I came up with a solution which in your case would be like this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(!check)
{
break;
}
}
case (something3):
{
break;
}
}
You see that instead of checking the conditions to go to the next case, we checked if we should ignore the current one.

Related

PHP - Is there a way to go from switch case to default?

is there a way to go from switch case to execute what is in default?
switch ($_REQUEST['action']) {
case 'edit':
if (isset($_REQUEST['id']) {
doSomething();
else {
goToDefault();
}
break;
case 'list':
...
break;
default:
doDefaultStuff();
break;
}
In the example, I want in the first case's else to go execute what is in default of switch.
Is that possible, or should I use different approach?
You can do that with goto, but I wouldn't recommend that.
$action = 'edit';
$id = null;
switch ($action) {
case 'edit':
if ($id) {
doSomething();
} else {
goto default_action;
}
break;
case 'list':
break;
default:
default_action: echo "doing default stuff";
break;
}
Demo: https://3v4l.org/X71e8
You can use switch(true) instead:
switch (true) {
case $_REQUEST['action'] === 'edit' && isset($_REQUEST['id']):
doSomething();
break;
case $_REQUEST['action'] === 'list':
// ...
break;
default:
doDefaultStuff();
break;
}

Codeigniter, using if statement .. how to make it more simple?

Codeigniter, using if statement .. how to make it more simple?
if($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="x")
{
Do action
}
else if ($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="y")
{
Do action
}
else if ($this->session->userdata('logged_in')!="" && $this->session->userdata('stts')=="z")
{
Do action
}
else
{
done
}
You could use a switch
if($this->session->userdata('logged_in')!="" ) {
switch ($this->session->userdata('stts'){
case "x":
Do action for x
break;
case "y":
Do action for y
break;
case "z":
Do action for z
break;
default:
Do action for othes
break;
}
} else {
// done
}
You can use switch case like this :
if($this->session->userdata('logged_in')!=""){
switch($this->session->userdata('stts')){
case "x":
// Do action
break;
case "y":
// Do action
break;
case "z":
// Do action
break;
default:
// Do action
break;
}
}
else{
//if session not set
}
Hope this helps!
if ($this->session->userdata('logged_in') != "" && in_array($this->session->userdata('stts'), ['x', 'y', 'z']))
{
//Do action
}
else
{
//done
}
using switch case you more beautify it.
if($this->session->userdata('logged_in')!="")
{
switch($this->session->userdata('stts'))
{
case "x":
//do some thing
break;
case "y":
//do some thing
break;
case "z":
//do some thing
break;
default:
//do some thing
}
} else {
// else condition
}
You can use a simple if/else and switch statement:
if ($this->session->userdata('logged_in') != "") {
switch ($this->session->userdata('stts')) {
case 'x':
// do action
break;
case 'y':
// do action
break;
case 'z':
// do action
break;
default:
// if neither x,y,z are stts
break;
}
} else {
// done
}

Multiple Switch Statements

Okay, I have one PHP file which needs to contain several switch($_GET['']) statements. For example: switch($_GET['id']), switch($_GET['open']), switch($_GET['number'])... Do I have to close it like:
switch($_GET['id'])
{
}
Or:
switch($_GET['open'])
{
};
One below another with or without semicolon?
This is my index.php:
It does not fully work. My php file is like this (Index.php):
<?php
// THE MAIN SITE
switch($_GET['open'])
{
default: include("Home-Page.php");
case 'Site': include("LetsStart/Pages/Home.php"); break;
case 'Links: switch ($_GET['topics'])
{
default: include("LetsStart/Pages/Links.php"); break;
case 'Tourism': include("LetsStart/Pages/Tourism.php"); break;
case 'Finance': include("LetsStart/Pages/Finance.php"); break;
case 'Health Care': include("LetsStart/Pages/HealthCare.php"); break;
}
break;
case 'About Us': switch ($_GET['details'])
{
default: include("LetsStart/Pages/AboutUs.php"); break;
case 'What We Do': include("LetsStart/Pages/WWD.php"); break;
case 'Our History': include("LetsStart/Pages/OurHistory.php"); break;
}
break;
}
// ENCYCLOPEDIA
switch($_GET['letter'])
{
case 'B': switch($_GET['term'])
{
default: include("LetsStart/Pages/TheEncyclopedia/Letter-B-Main.php"); break;
case 'Term 1': include("LetsStart/Pages/TheEncyclopedia/B/1.php"); break;
case 'Term 2': include("LetsStart/Pages/TheEncyclopedia/B/2.php"); break;
case 'Term 2': include("LetsStart/Pages/TheEncyclopedia/B/3.php"); break;
}
break;
}
?>
It keeps loading my home page and the first page from the second switch.
You do not need a semicolon after the closing bracket of a switch statement (same as an if statement).
You don't need semicolon, because the $_GET['id'] is a variable, not a string. Read this http://www.w3schools.com/php/php_switch.asp

Check if an array field is set in a switch statement

I have a switch statement set up which checks the value in an array field. I also want to perform slightly different logic if the array has no field with that name.
I can write the code like this, which works, but looks a little messy in my mind:
if (!isset($_GET['action']))
{
require('menu.html');
}
else
{
switch ($_GET['action'])
{
case 'debug':
require('core/actions/debug.php');
break;
case 'submit':
require('core/actions/submit.php');
break;
case 'admin':
header("Location: /login");
break;
}
}
But would it be possible for me to instead move the logic from the if statement and combine it with with my switch logic?
In JavaScript, I could do case undefined: ... as just one of the cases. Can I do something similar in PHP?
If $_GET['action'] is empty, or does have value, but its not any of the ones you want, you can do this.
switch ($_GET['action'])
{
.............
case "":
echo "empty or not setted";
break;
}
But if $_GET['action'] is not setted it will throw notices on every comparison (but it will enter in case '' anyway).
To not show the notices you could do:
switch (#$_GET['action'])
But please, don't do that!
You could do the super-switch-crazy way too:
switch(true){
case !empty($_GET['action']):
switch ($_GET['action'])
{
.............
}
break;
default:
echo "not setted or empty";
break;
}
Edit:
As #IQAndreas pointed out in the comments a interest solution could be:
switch (true)
{
case (!isset($_GET['action']):
require('menu.html');
break;
case ($_GET['action'] == 'debug'):
require('core/actions/debug.php');
break;
case ($_GET['action'] == 'submit'):
require('core/actions/submit.php');
break;
case ($_GET['action'] == 'admin'):
header("Location: /login");
break;
}
But the best way IMO to handle this situation is doing what you are already doing (checking if the var is empty or setted, before the switch..case)
if (isset($_GET['action'])){
switch ($_GET['action'])
{
.............
case "":
echo "empty";
break;
}
} else {
echo "not setted";
}

Roll to another case in PHP switch

For example I have this:
switch ($action)
{
case 'my_action':
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
Is the example above going to go through the switch again and then call the first case my_action if the second case second_action has $this_is_true variable set to true?
If this doesn't work, what would be an alternative?
You can try something like this:
switch ($action)
{
case 'my_action':
case 'second_action':
if ($this_is_true || $action=='my_action')
{
doSomething();
}
else
{
doSomethingElse();
}
break;
}
When $action is equal to 'my_action' it will run through the case, as it finds no break sentence then it will run through the second case until it finds the break sentence.
Please give a look to example #3 in http://php.net/manual/en/control-structures.switch.php to find out more about no breaking switch cases.
No it won't. Just call doSomething(); in the 'second_action' case.
I wouldn't use a switch in this simple case, but if it is very long then maybe:
$do = false;
switch ($action)
{
case 'my_action':
$do = true;
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
$do = true;
}
else
{
doSomethingElse();
}
break;
}
if($do) { doSomething(); }
Try to do it this way:
do {
switch ($action)
{
case 'my_action':
$this_is_true = false;
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
} while($this_is_true);
Do not forget to switch $this_is_true to false where it needed!
But this code is not beautiful... May be you better to refactor your code.

Categories