PHP switch() not working - php

I have this PHP switch:
<?php
$destination = isset($_GET['act']);
switch ($destination) {
default:
echo "test";
break;
case "manage":
echo "manage things";
break;
case "create":
echo "create things";
break;
}
?>
However, when I go to test.php?act=create, the output is manage things not create things.... and when I go to test.php?act=manage -- of course I get manage things...
So ... how do I fix this? Thank you

php's isset returns a boolean. So $destination is either true or false, not a string.
Try
if(isset($_GET['act']))
$destination = $_GET['act'];

Your problem is:
$destination = isset($_GET['act']);
isset returns either true or false, never any of the string values you are using.
You could use something like:
$destination = isset($_GET['act']) ? $_GET['act'] : '';

You have to use:
<?php
if(isset($_GET['act'])) $destination = $_GET['act'];
switch ($destination) {
case "manage":
echo "manage things";
break;
case "create":
echo "create things";
break;
default:
echo "test";
}
Or just use:
$destination = #$_GET['act'];

Related

Convert elses into switch with boolean return value

I am trying to convert this simple set of else statement into a more easily readable switch;
$parts_arr = explode('.', $_SERVER['SERVER_NAME']);
if (in_array('dev', $parts_arr)) {
DEFINE('APP_ENV', "dev");
} else if (in_array('staging', $parts_arr)) {
DEFINE('APP_ENV', "staging");
} else if (in_array('local', $parts_arr)) {
DEFINE('APP_ENV', "local");
} else {
DEFINE('APP_ENV', "live");
}
However I have completely drawn a blank. I can't use a foreach loop and use the string as the case as APP_ENV cannot be redefined.
You may only check the whole server name.
switch($_SERVER['SERVER_NAME']) {
case 'mysite.com':
case 'www.mysite.com':
DEFINE('APP_ENV', "live");
break;
case 'dev.mysite.com':
DEFINE('APP_ENV', "dev");
break;
case 'staging.mysite.com':
DEFINE('APP_ENV', "staging");
break;
case 'mylocalhost.local':
DEFINE('APP_ENV', "local");
break;
default:
exit;
}
You can't turn it into switch-case structure unless you're doing string comparaison.
Maybe something like this :
$str = array_pop(explode('.', $_SERVER['SERVER_NAME']));
switch($str)
{
case 'dev' :
DEFINE('APP_ENV', "dev");
break;
// en so on
}
Sunil Pachlangia's solution won't work because he is comparing an array and a string
<?php
$parts_arr = explode('.', $_SERVER['SERVER_NAME']);
switch (true) {
case in_array('dev', $parts_arr):
DEFINE('APP_ENV', "dev");
break;
case in_array('staging', $parts_arr):
DEFINE('APP_ENV', "staging");
break;
case in_array('local', $parts_arr):
DEFINE('APP_ENV', "local");
break;
default:
DEFINE('APP_ENV', "live");
break;
}
I think I'd tend to shy away from splitting strings and analysing array elements, as #Almo-Do commented Be specific in some kind of config file.
$environments = array(
'localhost' = > 'local'
, 'staging.mysite.com' > 'staging'
// etc - see? now you can comment some out
//, 'mysite.com' => 'live'
);
Then simply
define ('APP_ENV', $environments[$_SERVER['SERVER_NAME']]);
Or even be a bit more defensive prior to that, something like :
if (!array_key_exists($environments[$_SERVER['SERVER_NAME'])) die('suitable msg');

Switch statement using strstr always validates as true?

I have the following switch statement.
The URL contains a referral ID e.g twitter, facebook or an email e.g mail#mail.com. This is stored as $ref
I have the following switch statement:
switch ($ref) {
case "twitter":
echo "twitter";
break;
case "facebook":
echo "facbeook";
break;
case "blog":
echo "blog";
break;
case strstr($ref,'#'):
echo "email = ".$ref;
default:
echo "no referral found";
break;
}
However if URL is passed with nothing (e.g just www.mything.co.uk) then I wish to go to the default case.
Instead, I get the following output:
email = no referral found
Why does the default also include the text I set for case strstr($ref,'#') ?
OP question: "Why does the default also include the text I set for case strstr($ref,'#') ?"
Answer: there's no break; following the output, and thus falls through to the default case.
UPDATE: Addressing the issue of putting a statement within a case, I'm also including an easy work-around:
switch ($ref) {
case "twitter":
echo "twitter";
break;
case "facebook":
echo "facbeook";
break;
case "blog":
echo "blog";
break;
default:
if (strstr($ref,'#')) {
echo "email = ".$ref;
} else {
echo "no referral found";
}
break;
}
When $ref is an empty String, then strstr($ref,'#'); returns an empty string too, this is why the case strstr($ref,'#'): matches the switch input $ref.
The problem is, you can't even use a email validation function like
filter_var($ref, FILTER_VALIDATE_EMAIL)
That would return false in case of an empty input instead of an empty string, but switch does loose comparison, meaning that an "" == false would return true:
http://php.net/manual/en/types.comparisons.php#types.comparisions-loose
Thus the only solution I see is to use an if statement using the === operator:
if($ref == 'twitter') {
echo "twitter";
} else if($ref == 'facebook') {
echo "facbeook";
} else if($ref == 'blog') {
echo "blog";
} else if($ref === filter_var($ref, FILTER_VALIDATE_EMAIL)) {
echo "email = ".$ref;
} else {
echo "no referral found";
}
That's because your test is performed like if ($ref == strstr($ref, '#')), where strstr returns false which equals an empty string. You cannot really use dynamic comparisons in switch statements. Use if..else if you need that. Alternatively, abuse switch a bit:
switch (true) {
case $ref == 'twitter':
..
case strstr($ref, '#'):
..
}
That will work:
case (strstr($ref, '#') ? true : false):
But it's not really good of practice.

Assign result of function within switch to variable

here's some pseudo-code (it's not written correctly, the point of my ? is the variable, not the switch):
switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}
How should this be written:
$variable = result of function in case 1.
Your switch statement is wrong . It requires a break keyword between every case
$action = 1;
$result = "Success";
switch ($action) {
case 1:
$variable = $result;
echo $variable;//prints Success
//this is a function
break; // like this
case 2:
//this is a function
break;//
//etc.
}
Just run the function(s) (you can pass args in too) as part of the code within the case / break blocks like this :
$action = 1;
switch ($action) {
case 1:
$variable = someFunctionOne();
break;
case 2:
$variable = someOtherFunctionTwo();
break;
//etc.
}
how to variable the result of php switch.
ex:
<?php
//variables of cases
$var_1 = 1;
$var_2 = 2;
$var_3 = 3;
$var_0 = 0;
//end variables of cases
//action variable
$action = 10;
//end action variable
//start switch
switch ($action) {
case "1":
echo "$var_1;";
break;
case "2":
echo "$var_2;";
break;
case "3":
echo "$var_3;";
break;
default:
echo "$var_0;";
}
//receives the value of the switch.
$switch_result = get_result_case;
//in this my example I need to enter the value of the case in a variable.
?>
in this my example I need to enter the value of the case in a variable.

switch case isset triggered when case is -not- set

--Let me add this. This code works for me the way it is. I just do not know why it works.--
I can't figure this out.
switch ($_SERVER['QUERY_STRING']) {
case isset($_GET['test0']):
echo "test0<br>";
break;
case isset($_GET['test1']):
echo "test1<br>";
break;
case isset($_GET['test2']):
echo "test2<br>";
break;
case isset($_GET['test3']):
echo "test3<br>";
break;
case isset($_GET['test4']):
echo "test4<br>";
break;
default:
echo "no test<br>";
break;
}
When the url is index.php?test0, "test0" is shown.
When the url is index.php?test4, "test4" is shown.
When the url is index.php?test999, "no test" is shown.
When the url is index.php?tes, "no test" is shown.
When the url is index.php?, or index.php, "test0" is shown.
Why is this? The condition is not met, so should the default not be shown?
switch can't be used this way. isset() returns true or false, not something (a string, an int, etc) you can match against. What you are basically doing is:
switch ($_SERVER['QUERY_STRING']) {
case true:
echo "test0<br>";
break;
case true:
echo "test1<br>";
break;
case false:
echo "test2<br>";
break;
case false:
echo "test3<br>";
break;
case true:
echo "test4<br>";
break;
default:
echo "no test<br>";
break;
}
cases are considered from top to bottom. In this case, $_SERVER["QUERY_STRING"] is automatically type-converted to bool (which will return true in this case). The first case it sees would be test0, so it echos that. If you do that for test0-4, it will give you the false illusion that this code is working as intended, while it's not considering the edge cases.
The only way you can achieve what you want is by using multiple ifs, or by redesigning your application.
When the url is index.php?, or index.php, "test0" is shown.
Why is this? The condition is not met, so should the default not be shown?
Like a good question, your question as well contains the answer already.
You already have realized that the condition must be met even you think it is not met. Therefore you ask. So let's see which condition is met:
case isset($_GET['test0']):
echo "test0<br>";
break;
This is a test for isset($_GET['test0']) and we know with the request that this is FALSE. So this test tests for FALSE.
Now let's see against what this tests:
switch ($_SERVER['QUERY_STRING']) {
That is $_SERVER['QUERY_STRING']. So if $_SERVER['QUERY_STRING'] is FALSE the test0 will be output.
Because switch { case:} in PHP does loose comparison, the empty string $_SERVER['QUERY_STRING'] is FALSE. This is why you see the output.
Easy if you know why, right? And all so logical.
And what you wanted to test against was not $_SERVER['QUERY_STRING'] but just TRUE:
switch (TRUE)
{
case isset($_GET['test0']) :
...
}
This gets the job done, too.
<?php
$q = $_SERVER['QUERY_STRING'];
if(!empty($q) && isset($q) && strlen($q) >0 ){
$url = $q;
switch ($url){
case true;
echo $url;
break;
}
}
else {
echo "no test<br>";
}
what about
$found = false;
for($i=0;$i <=4; $i++){
if( isset($_GET['test'.$i]) ){
echo "test".$i;
$found = true;
}
}
if(!$found){
echo "no test";
}

if statement switch

<?php
if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];
switch($content) {
//The default page
default:
include_once('main.php');
break;
//The news related stuff
case 'news':
include_once('news.php');
break;
//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
}
?>
index.php?user_id=id won't work. Any ideas?
Maybe you intended this:
if (isset($_GET['user_id'])) $content = 'user_id';
Instead of:
if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];
Then the switch would execute the user_id case and include profile.php.
the default case needs to be the last one.
switch($content) {
//The news related stuff
case 'news':
include_once('news.php');
break;
//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
//The default page
default:
include_once('main.php');
break;
}
Plus, you said that this doesn't work: index.php?user_id=id
The $content variable is being populated by the value of user_id in the _GET string, therefore if you wanted to see something other than the default page, you'd have to do one of these:
index.php?user_id=news
index.php?user_id=user_id
This probably isn't how you want it to behave, but given your code, this is what it's doing...

Categories