It appears there's something wrong in my Switch Case logic as it always falls through to default.
echo $platform;
switch($platform){
case "Gaming / Xbox 360 Games":
$internalPlatform = "MXT";
break;
case "Gaming / Nintendo DS Games":
$internalPlatform = "NDS";
break;
default:
$internalPlatform = "MISC";
break;
}
echo $internalPlatform;
Where $platform = "Gaming / Xbox 360 Games" and $internalPlatform = "MISC".
Any ideas on what is causing it to fall through?
Nothing wrong with the logic above.
Thanks to Paolo for pointing out to strip tags & invisibles out the variable. var_dump helped me find that out!
Mybe you missed a prefix or suffix space
Try to comper the output of this echo with the case string.
echo "$platform";
Related
Doing this in PHP
I have users input an abbreviation for a city, that gets saved as a string, for example:
ANA
BOS
VAN
These stand for: Anaheim Boston Vancouver
I want it so that when I get the input/string, it changes the string name to it's full name.
ANA -> Anaheim
BOS -> Boston
VAN -> Vancouver
What is the best way to go about this? Thank you all, greatly appreciated.
Peut22's answer ist working, but i think it is better to store the pairs in an array to be more eaily maintainable. You could also get this array from a database or config file.
$names = array(
"ANA" => "Anaheim",
"BOS" => "Boston",
"VAN" => "Vancouver",
);
if(array_key_exists((string)$userinput, $names)) {
echo $names[$userinput];
} else {
echo "invalid";
}
update: fixed, so there is no "undefined index" - notice in case you got such a low error_reportinglevel set:
$names["ANA"] = "Anaheim";
$names["BOS"] = "Boston";
$names["VAN"] = "Vancouver";
function getFullName($input, $names)
{
//turning all input into upper case letters:
$input = strtoupper($input);
//making sure input is maximum 3 characters long:
$input = substr($input, 0, 3);
if(!array_key_exists($input, $names))
{
return "*ERROR: Unknown City Abbreviation*";
}
else
{
return $names[$input];
}
}
//usage:
$user_input = "bos";
echo getFullName($user_input, $names);
I think you would get a good result by using a switch, which returns the full name if you happend to get an abbreviation, of the full input string if the name is not recognized.
An example in pseudo code would be:
Switch string
case "ANA" : string = "Anaheim"
case "BOS" : string = "boston"
end
EDIT: if you're using php, the code would then look like :
switch ($i) {
case "ANA":
$i="anaheim"
break;
case "BOS":
$i="boston"
break;
}
I cannot get my head around this. There might be a simple solution and the problem addressed already, but I could not find an answer.
<?php
$string = '';
$array = array(1, 2, 3);
foreach($array as $number) {
switch($number) {
case '1':
$string .= 'I ';
case '2':
$string .= 'love ';
case '3':
$string .= 'you';
}
}
echo $string;
?>
As you might have guessed, the sentence produced should be: I love you
But that is the actual output: I love youlove youyou
How is this even possible, when the switch is only triggered thrice.
Meanwhile I know that the problem can be fixed with a >break;< after each case. But I still do not understand why this is necessary.
I would be very happy, if someone could explain to me what PHP is doing.
Happy Valentines!
When case 1 is matched it will also execute case 2 and 3 unless there is a break.
So each time through the loop the matched case and everything following is executed.
First time: case 1, 2, 3
Second time: case 2, 3
Third time: case 3
For reference, here's an extract from the PHP documentation on switch, which explains it better than I probably would. :)
It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:
<?php
switch ($i) {
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>
Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).
When you don't specify a break the code execution will fall-through to the next case.
As an example an exam paper with a score out of 10 could be graded as such:
switch ($score)
{
case 10:
// A+ when score is 10
echo 'A+';
break;
case 9:
case 8:
// A when score is 8 or 9
echo 'A';
break;
case 7:
case 6:
// B when score is 7 or 6
echo 'B';
break;
case 5:
case 4:
case 3:
// C when score between 3 and 5
echo 'C';
break;
default:
// Failed if score is less than 3
echo 'Failed';
}
i have a list of numbers that range from good ones like 51 and 102. when its over a thousand it writes it as 2,012 or 9,216. when it gets over tenthousand it writes it as 24.1k, 87.9k. when it gets over 1 million it writes as 1.4m or 142.5m.
problem is i dont know how to convert from this to readable numbers. for example, 142.5m should be 142500000.
as a first step i tried using this:
function cleanPrice($pris)
{
return preg_replace('/\D/', '', $pris);
}
cleanPrice($pris);
it didnt go too well. i was trying to get rid of the , and . and the m and k. but got nowhere.
is there a function or something built into php that does this? i know number_format exist but it only goes one way, and that way is sadly the wrong way i want to go
function cleanPrice($pris) {
$last = substr($pris, strlen($pris)-1);
switch($last) {
case "k": $scale = 1000; break;
case "m": $scale = 1000000; break;
default: $scale = 1;
}
return preg_replace('/[^-0-9.]/', '', $pris) * $scale;
}
DEMO
Code:
switch ($_GET['operation']) {
case "professionist":
case "company":
case "student":
echo "ok<br>";
case "professionist":
echo "inprofessionist<br>";
break;
case "company":
echo "incompany<br>";
break;
default:
echo "Meh!<br>";
break;
}
My goal is to execute some (common to professionist/company/student) code first, and then execute the rest of the code depending on the operation...
The problem is that the first 3 cases works perfectly, but then, if for example the operation is "company", the switch go on "professionist" case, what i'm doing wrong? How can improve that? Thanks in advance...
That's how switch works. If you always need to print "ok" then move it outside switch:
$op = $_GET["operation"];
if (in_array($op, array("professionist", "company"))) {
echo "ok<br>";
}
switch ($op) {
case "professionist":
echo "inprofessionist<br>";
break;
case "company":
echo "incompany<br>";
break;
default:
echo "Meh!<br>";
break;
}
Only the first case with a given value will ever be targeted by a switch. (And in fact, a switch only ever goes to a single case per execution - fallthough just lets you have multiple cases share some code, but only one of them is what's actually triggered.)
Thus, it doesn't make sense to have multiple casees with the same value that they're looking for - a switch statement isn't the same thing as a series of independent ifs; it's a mapping of jump targets.
Essentially, let's say you have a switch like this:
w; // 001
switch ($foo) { // 002
case 'a':
w; // 003
case 'b':
x; // 004
break; // 005
case 'c':
y; // 006
}
z; // 007
What actually winds up effectively happening is that you get some code effectively like this (note: highly simplified from how this actually works):
001 v
002 jump based on $foo: [a -> 003, b -> 004, c -> 006]
003 w
004 x
005 jump to 007
006 y
007 z
and then that program just gets run from top to bottom.
In the case where $foo is 'a', it jumps to 003 and runs from there, which means that it winds up doing v,jump,w,x,z in total.
In the case where $foo is 'b', it jumps to 004 and runs from there, which means that it winds up doing v,jump,x,z in total.
In the case where $foo is 'c', it jumps to 006 and runs from there, which means that it winds up doing v,jump,y,z in total.
Is it possible to use "or" or "and" in a switch case? Here's what I'm after:
case 4 || 5:
echo "Hilo";
break;
No, but you can do this:
case 4:
case 5:
echo "Hilo";
break;
See the PHP manual.
EDIT: About the AND case: switch only checks one variable, so this won't work, in this case you can do this:
switch ($a) {
case 4:
if ($b == 5) {
echo "Hilo";
}
break;
// Other cases here
}
The way you achieve this effectively is :
CASE 4 :
CASE 5 :
echo "Hilo";
break;
It's called a switch statement with fall through. From Wikipedia :
"In C and similarly-constructed languages, the lack of break keywords to cause fall through of program execution from one block to the next is used extensively. For example, if n=2, the fourth case statement will produce a match to the control variable. The next line outputs "n is an even number.". Execution continues through the next 3 case statements and to the next line, which outputs "n is a prime number.". The break line after this causes the switch statement to conclude. If the user types in more than one digit, the default block is executed, producing an error message."
No, I believe that will evaluate as (4 || 5) which is always true, but you could say:
case 4:
case 5:
// do something
break;
you could just stack the cases:
switch($something) {
case 4:
case 5:
//do something
break;
}
switch($a) {
case 4 || 5:
echo 'working';
break;
}