how can i display one switch case at a time - php

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;
.....
}

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.

switch case not working correctly

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.

PHP switch function not show true result

I have this function for show type name :
function is_typename($type){
switch ($type){
case 1:
echo "Bring an extra $500";
break;
case 2:
echo "Bring an open mind";
break;
case 3:
echo "Bring 15 bottles of SPF 50 Sunscreen";
break;
case 4:
echo "Bring lots of money";
break;
case 5:
echo "Bring a swimsuit";
break;
}
}
No for show link I have :
echo ''.$title.'';
in action i see :
Bring an extra $500titeltest
Problem: $type and typename show in outer href and <a></a>. how do fix this?
instead of echo use return,
example
case 1:
return "Bring an extra $500";
break;

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 :)

PHP Switch statement displaying case label in echo statement

I have used the following SWITCH statement to ECHO one of three CASE's. However when I do this the output to the web page shows the CASE label then the actual echo-ed statement.
$sc_stk_poa is a boolean field of either 0 or -1.
$sc_stk_prc_stanard is a price field in the MySQL database.
echo $x = $sc_stk_poa;
switch($x) {
case 0: echo "£{$sc_stk_prc_standard}";
break;
case -1: echo "POA";
break;
default: echo "";
}
The output I get in the webpage is something like: -1POA or 0£59.14 depending which case is selected to output.
Write less convoluted things :
<?php
switch ($sc_stk_poa)
{
case 0:
echo "£{$sc_stk_prc_standard}";
break;
case -1:
echo "POA";
break;
default:
echo "";
}
There was no need for variable $x, was it?
It's not displaying the label. You have an echo here: <?php echo $x=$sc_stk_poa;. This is the one outputting the value of x, which is used in your cases.

Categories