PHP switch function not show true result - php

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;

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.

Undefined variable on switch via get method

I have a cms admin section with the option comments; when i press comments on the admin menu it should take me to comments.php which includes the code below; by default it should load all my comments on a grid, however when i press comments its not displaying anything the page is blank ? My code:
<?php
if(isset($_GET['source'])){
$source1=$_GET['source'];
if(!empty($source1)){
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
}
}
?>
when u load comments.php the url doesnt sent any parameter via get, on your code you are checking if the source is not empty which on your case its empty at first. Then preform switch statment, in your case it will not preform the switch as the source is empty and thats why you are not seeing anything. You can fix that by adding an else on your if and include view_all_comments.php or like the code below:
<?php
$source1=isset($_GET['source']);
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
?>

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

php: how to search the url in a switch case and echo always something different for every url?

i got the code below to find the url of the site and this works. But I want a switch statement to search this url in a list and echo something else in for every url.
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
As far as I understand the question, this looks like what you are looking for:
switch ($actual_link) {
case 'http://domain/something1/':
echo 'Something 1';
break;
case 'http://domain/something2/':
echo 'SomeSthing 2';
break;
default:
echo 'default';
}

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