SWITCH-statement two case with same condition and also different condition - php

Hi I wonder if this could be done in switch case. Here's the example code I wanna do
switch($name)
case "Dog":
$pic = "/images/itscute.jpg";
$info ="four legs";
break;
case "Cat":
$pic = "/images/cat.jpg";
$info ="four legs";
break;
case "bird":
$pic = "/images/bird.jpg";
$info = "two legs";
break;
Now you can see that both of dog and cat have the same value of $info.
Does it possible for me to make $info only one for both of them like this
switch($name)
case "Dog":
case "Cat":
$info ="four legs";
break;
case "bird":
$info = "two legs";
break;
then again I don't know how to place the $pic if the code like this.
EDIT : $pic at dog is not "/images/dog.jpg";
EDIT2 : added more case to be more clear question

For any variables that contain the same value why use a switch at all? Just define those variables before the switch statement. Use the switch statement only for variables that contains different values.
EDIT
In that case there is no reason why you can't use 2 switch statements, like this:
switch(strtolower($name))
{
case "dog":
$pic = "/images/itscute.jpg";
break;
case "cat":
$pic = "/images/cat.jpg";
break;
case "bird":
$pic = "/images/bird.jpg";
break;
}
switch(strtolower($name))
{
case "dog":
case "cat":
$info = "four legs";
break;
case "bird":
$info = "two legs";
break;
}
I do recommend that you use strtolower(), like my example shows, to avoid any case problems. You can use any number of switch statements as you need.
As for turning "dog" into "Dog's" just add the "'s" to the variable, like this:
$name = ucwords($name . "'s");

Since the variation of $pic between two cases is dependent on the value of $name, you can use $name itself:
switch($name) {
case "Dog":
case "Cat":
$pic = "/images/" . strtolower($name) . ".jpg";
$info ="four legs";
break;
}

Related

After adding detection of browser language, the language href anchor (hl) stops working

I know how to detect language browser in PHP and also how to detect the language href anchor for switching to another.
My old codes which doesn't have locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);:
if (isset($_GET['hl']))
{
$langOption = $_GET['hl'];
}
else
{
$langOption = '';
}
switch ($langOption):
case 'ca':
$language = 'cat';
break;
case 'el':
$language = 'el';
break;
case 'en':
$language = 'en';
break;
case 'en_GB':
$language = 'en_GB';
break;
case 'es':
$language = 'es';
break;
case 'fr':
$language = 'fr';
break;
case 'ka':
$language = 'ka';
break;
case 'nl':
$language = 'nl';
break;
case 'pt_PT':
$language = 'pt_PT';
break;
case 'pt_BR':
$language = 'pt_BR';
break;
case 'ro':
$language = 'ro';
break;
default:
$language = 'pt_BR';
break;
endswitch;
require_once("idiomas/{$language}.php");
These old codes worked very for language href anchors. Like:
<a class="waves-effect" href="?hl=en_GB" name="en">English</a>
<a class="waves-effect" href="?hl=pt_BR" name="pt_BR">Português Brasileiro</a>
I changed to codes to condense, economise and shorten the codes. Here are new codes:
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$lang = $_GET['hl'];
}
else
{
$lang = '';
}
switch ($lang):
case $language:
$language = $language;
break;
default:
$language = $language;
break;
endswitch;
require_once("idiomas/{$language}.php");
Only the detection of browser language worked very well, but the language href anchors (hl) do not work, because if you switch to Portuguese, the page still in English.
Similar like:
switch ($lang):
case "en":
$language = "en";
break;
case "en_GB":
$language = "en_GB";
break;
case "pt_BR":
$language = "pt_BR";
break;
case "pt_PT":
$language = "pt_PT";
break;
endswitch;
But I wouldn't like to repeat these old codes. I want to keep condensing and shortening the same new codes.
I don't understand what the switch statement is doing here?
The reason it's not working is because you're not using the $lang variable for anything, it's just setting $language to itself for all cases.
I think this would do what you want :
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$language = $_GET['hl'];
}
As query params can be modified by the user, I'd also suggest using in_array to check that the value is something expected.

PHP Getting all file names in dirctory for page switching

Right now I have all my pages under a switch case for when a visitor uses whatever.php?p=pagename
Here is how it is currently
switch($page)
{
case 'about':
include('pages/about.php');
break;
case 'contact':
include('pages/contact.php');
break;
case 'edb':
include('pages/edb.php');
break;
case 'eluna':
include('pages/eluna.php');
break;
case 'mercsys':
include('pages/mercsys.php');
break;
case 'pastebin':
include('pages/pastebin.php');
break;
case 'projects':
include('pages/projects.php');
break;
case 'sites':
include('pages/sites.php');
break;
case 'soon':
include('pages/soon.php');
break;
case 'sqlgen':
include('pages/sqlgen.php');
break;
case 'wcms':
include('pages/wcms.php');
break;
case 'add':
include('pages/add.php');
break;
case 'edit':
include('pages/edit.php');
break;
case 'delete':
include('pages/delete.php');
break;
case 'moveAnnouncement':
include('pages/moveAnnouncement.php');
break;
default:
include('pages/404.php');
}
My question is, How can I shorten this down to a foreach loop and use the page names for each .php in the pages/ directory without having to add each individual one or any future pages?
upd: like #svrnm adviced, you can do some security checks if you not did it before:
$filename = realpath('pages/' . $page . '.php');
if($filename && file_exists($filename)) {
include($filename);
}
or/and you can build files whitelist first:
$whitelisted = glob("*.php");
if(in_array($page . '.php', $whitelisted) && file_exists($filename)) {
include($filename);
}

How can I pass this variable and check if it matches?

How can I correct/simplify this and put it in an array?
A link is passing: somelink.php?w=a (or b,c,d)
I want the page (somelink.php) to determine if "w" is set, and if set and the var matches, include the specified page.
<?php
if(isset($_GET['w'])&&($GET['w'] == "a")){include("1.htm");}
if(isset($_GET['w'])&&($GET['w'] == "b")){include("2.htm");}
if(isset($_GET['w'])&&($GET['w'] == "c")){include("3.htm");}
if(isset($_GET['w'])&&($GET['w'] == "d")){include("4.htm");}
else{include("1.htm");}
?>
try using:
$w = $_GET['w'];
if(isset($w)) {
switch(strtolower($w)) {
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
default:
include("not-found.htm");
break;
}
}
Use a switch statement:
if(isset($_GET['w']))
{
switch($_GET['w'])
{
case 'a': include("1.html"); break;
case 'b': include("2.html"); break;
case 'c': include("3.html"); break;
case 'd': include("4.html"); break;
default: include("1.html"); break;
}
} else {
include("1.html");
}
how about a simple array
$x=array('a'=>'1.html','b'=>'2.html');
then
include $x[$GET['w']];
Like this:
if(isset($_GET['w'])){
switch($_GET['w']){
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
}
}
But I wouldn't do it that way. I'd make it so that the name of the page corresponds to the value being retrieved from the $_GET variable. That way you could do something like this.
if(!empty($_GET['w'])){
include($_GET['w'] . ".htm");
}
Of course, you'd want a little filtering of the $_GET var too to make sure it doesn't get something you don't want there. Maybe like this.
$acceptable_values = array("some","acceptable","values");
if(!empty($_GET['w']) && in_array($_GET['w'],$acceptable_values) ){
include($_GET['w'] . ".htm");
}
As I'm sure you are aware, passing variables directly into include statements or database queries is a TERRIBLE idea. See here for why in this case.
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
You could do a few things, lets take a look at some of them.
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
switch($webpage)
{
case 'b':
include '2.html';
break;
case 'c':
include '3.html';
break;
case 'd':
include '4.html';
break;
default:
include '1.html';
break;
}
Or we could use arrays
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
$included_pages = array(
'a' => '1.htm',
'b' => '2.htm',
'c' => '3.htm',
'd' => '4.htm',
);
// Check inside our array
if(array_key_exists($webpage, $includes))
{
include $included_pages[$webpage];
}
else
{
// Couldn't find the site
include '1.htm';
}

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.

Check several field to execute switch case

I have a code to check through several fields to execute certain code as below:
switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
But it does not work.
Anyone can help?
Switch support one value only. Only IF condition can have OR and AND condition.
$tags = get the tag value.
switch($tags){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
The following should do the trick for you:
<?php
$tag1='satay';
$tag2='test2';
$tag3='digitalmarketing';
function isItThere($myTag)
{
switch($myTag)
{
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
echo $imgput;
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
echo $imgput;
break;
// etc etc
}
}
for($i=1;$i<4;$i++)
{
isItThere(${'tag'.$i});
}
?>
I have basically set up a small function that contains the switch statement and written a simple loop to test the variables.
As I said in my comment, you can't use more than one variable in the switch statement, but this will provide you a nice clean workaround to do the same thing without the need to write many long statements.

Categories