switch case not working correctly - php

I try to case value inside switch. According to this code, it should be echo "Your favorite color is red", but it doesn't. When I run this code, it give a result:
Your favorite color is neither red, blue, nor green! but red colour.
Here is my code
<?php
$response = null;
switch ($msg) {
case "red":
echo "Your favorite color is red";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green! but " . $msg . " colour.";
}

To mark this as answered, as suggested in my comment, you need to validate your input to ensure that no white spaces are throwing off your switch statement, to do so you can use trim.
Another suggestion would be to use isset and empty on your incoming $_POST too to ensure that it is being sent. You should always sanitize your user input too as someone could be passing malicious data through your request. In your current instance they wouldn't be able to do much lasting damage but when working on larger projects it is extremely important to validate and sanitize any incoming data. Never trust user input.

Related

How to set values to a variable inside switch case, and call that variable outside switch in Php

I have some dynamic values to place in variable, but for testing purpose I am taking dummy values, The problem is that I want to set value to the variable inside switch case by checking different conditions and call that variable outside switch case to print out put but I am not getting any value while echo .
So how to print the value by calling that variable?
<?php
$favcolor = "red";
$color_val;
switch ($favcolor) {
case "red":
return $color_val="Your favorite color is red!";
// echo "Your favorite color is red!";
break;
case "blue":
return $color_val="Your favorite color is blue!";
break;
case "green":
return $color_val="Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
echo $color_val;
You need to remove the "return" keyword from your switch. It doesn't do what I think you are intending for it to do there.

how can i use IF commands to show different information on a table?

i am trying to show information using IF/ELSEIF commands
i have A through Z along the top of the page and want to show a table with all results starting with each letter
for example i have
<a href='?a'>A</a>
<?php
if($_GET == a)
{
echo "<table><tr><th>[picture]</th><th>information</th></tr>";
}
?>
i want to show a table with all the information, how would i do this using IF/ELSE commands? is there a better way of doing this without going to a different page?
thanks in advance for any help
I think it would be easier/cleaner to use a switch-case instead of if-else for your purpose here.
First off, try changing the links to something like this:
<a href='?l=a'>A</a>
and
<a href='?l=b'>B</a>
Then you should try to access the chosen letter with something like this:
<?php
$sLetter = null;
if (isset($_GET['l'])) {
$sLetter = strtolower($_GET['l']);
}
switch ($sLetter) {
case 'a':
echo "Information related to A";
break;
case 'b':
echo "Information related to B";
break;
// Continue in a similar way for the remaining letters
default:
echo "No information..."; // or perhaps show all A-Z information
break;
}
Note: For testing purposes, this is okay. But Superglobals should always be validated and sanitised to make your application more secure.

how can i display one switch case at a time

If i have a case like this
<?php
$favcolor = "red"; /*<<<<<<<<<<<<< this is just a random example >>>>>>>>>>> */
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "red":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
How can i display one case as a result and then another when clicked next; or something?
This is just an idea that will improve my website
You can't have two cases with the same value.
Well, I mean, you can. However, it's only ever going to use the first instance of "red" that it finds when it traverses the switch.
You would either have to have two separate functions, one called on the original page, and the other called when "Next" is clicked, OR you would have to have an additional variable available to your switch function.
$favcolor = 'red';
switch($favcolor){
case "red":
if(isset($next) && ($next == TRUE)) {
echo 'Your favorite color is red and you clicked "Next"!';
} else {
echo 'Your favorite color is red!';
}
break;
.....
}

Can I use an if statement inside of a switch in php?

Heres what I want to do:
I have a variable named $user, where the information is being POSTED from a form. If the form data is empty I want to do one thing, if the form data is just a comma or symbol I want to do another thing, and if the form data is correct I want to do a third thing. This is how I have it set up, will this work? If not, what can I do to make it work?
switch($user){
case 1:
if(empty($user)){
echo "<h2>"."<strong>"."You entered NOTHING, PLEASE Re-Enter"."</strong>"."</h2>";
}
case 2:
if($user == ",") {
echo "<h2>"."<strong>"."You did not enter any valid names/abbreviations to search, PLEASE Re-Enter"."</strong>"."</h2>";
}
default:
print "==========================================="."<br />";
tester($company, $user);
print "==========================================="."<br />";
print "========The following were NOT found====="."<br />";
notFound($company, $user);
print "==========================================="."<br />";
}
Right now it is automatically going to default. It runs, but I want integrate the two qualifiers, how would I go about doing that?
You need to put break at the end of each case in a switch statement or it falls through to the next case. I also think you may be confused about the way switch works. If you're switching on $user, case matches against the value of $user--so you don't even need if-statements.
switch($user){
case "":
echo "<h2>"."<strong>"."You entered NOTHING, PLEASE Re-Enter"."</strong>"."</h2>";
break;
case ",":
echo "<h2>"."<strong>"."You did not enter any valid names/abbreviations to search, PLEASE Re-Enter"."</strong>"."</h2>";
break;
default:
print "==========================================="."<br />";
tester($company, $user);
print "==========================================="."<br />";
print "========The following were NOT found====="."<br />";
notFound($company, $user);
print "==========================================="."<br />";
}

Displaying correct answer for PHP quiz game

I am creating my first PHP script that tests the user on cranial nerve association; specifically the name of the cranial nerve is displayed to the user and they are supposed to click on the correct number corresponding to the name of the cranial nerve.
I will post my code first and my question after:
<?php
$generated_nerve_number = mt_rand(1,12);
switch($generated_nerve_number) {
case '1':
echo "Olfactory";
break;
case '2':
echo "Optic";
break;
case '3':
echo "Oculomotor";
break;
case '4':
echo "Trochlear";
break;
case '5':
echo "Trigeminal";
break;
case '6':
echo "Abducens";
break;
case '7':
echo "Facial";
break;
case '8':
echo "Vestibulocochlear";
break;
case '9':
echo "Glossopharyngeal";
break;
case '10':
echo "Vagus";
break;
case '11':
echo "Accessory";
break;
case '12':
echo "Hypoglossal";
break;
}
?>
<html>
<head>
<title>Cranial Nerves Test</title>
</head>
<body>
<p>Select the cranial nerve number below associated with the name of the cranial nerve given above:</p>
<form action="cranial.php" method="POST">
<?php
echo "Cranial Number: ";
for($i = 1; $i <= 12; $i++) {
echo "<input type=\"submit\" name=\"nerve_$i\" class=\"nerve_number\" value=\"$i\">";
}
?>
<?php
$submit = (isset($_POST['nerve_' . $i])) ? $_POST['nerve_' . $i] : NULL;
if($submit) {
$selected_nerve_number = $_POST['nerve_' . $i];
if($generated_nerve_number == $selected_nerve_number) {
echo "That is correct!";
} else {
echo "Sorry that is incorrect.";
}
}
?>
</form>
</body></html>
A quick overview of my thought process: I am randomly generating a number between 1-12 and using that number in a switch statement to echo the name of the cranial nerve. I used a for loop to generate submit buttons with names that contain the number of the cranial nerve corresponding to its displayed value. Lastly, my plan on checking whether the answer is correct or not is to use an if statement comparing the randomly generated number to the number selected and, if this is true, output a message saying that they were correct.
This is where the problem comes in: when I click any of the buttons, whether its the correct answer or not, the page just refreshes without giving any feedback on whether the answer was right or wrong. Can someone please point out the flaw?
Additionally, if there is a more optimal way of doing something in this script please let me know.
Thanks in advance.
There is a slight problem with this approach. Your script is actually having two steps to consider: first, sending a page to the client with a randomly chosen cranial nerve, then comparing a choice with what was randomly specified.
If you wish your script to do this, you must add some logic so that it will know it have to react differently.
if (isset($_POST))
{/*compare the user choice with what you had sent.
you will have to do add an input to your form containing the random value AND the chosen value, or you eon't be able to compare them*/}
currently, you're trying to do all this at once, eg, you're comparing the return value of your user before you even receive it in your post! Understand that once sent to the client (browser) your page is not linked anymore to the php. So here:
if($generated_nerve_number == $selected_nerve_number) {
you're actually comparing the random number you just generated, with the answer you will have from your user in the FUTURE, since he has not even seen it on screen yet :)

Categories