PHP Switch statement error - php

<?php
include 'db.php';
$mail=$_SESSION['session_u_e_mail'];
if(!isset($_GET['edit']))
{
$_GET['edit']=0;
}
switch($_GET['edit'])
{
case 1: echo "Value";
break;
case 2: echo "Value";
break;
default: echo "Value";
break;
}
I m facing problem that the values in every case is echoed twice.

I've just grabbed the
if(!isset($_GET['edit']))
{
$_GET['edit']=0;
}
switch($_GET['edit'])
{
case 1: echo "Value";
break;
case 2: echo "Value";
break;
default: echo "Value";
break;
}
this portion and pasted onto codepad and it works fine.
Here is the working version: http://codepad.org/P8bhL5vl
It outputs just "Value".

There is nothing in this code that would even output your default value. No matter the input, this outputs Value. Look in db.php or the files included by it for debugging echoes.

Related

Variable scope in php in 2 block codes

In a webpage in top i use this code:
<?php
switch ($urlcomecatid) {
case "95":
$target_cat1='96';
$target_cat1_name = "A";
break;
case "96":
$target_cat1='95';
$target_cat1_name = "B";
break;
?>
and in another down part the page i use these variable
<?php
echo "<p class='pagefooterlifestyle' align='center'>
<a href='../lifestyle/lifestylesub.php? catid=$target_cat1'>$target_cat1_name</a></p>";
?>
BUT i get error undefined variable for
$target_cat1
$target_cat1_name
please let me know that what is the problem?
Initialize your variables:
switch ($urlcomecatid) {
case "95":
$target_cat1='96';
$target_cat1_name = "A";
break;
case "96":
$target_cat1='95';
$target_cat1_name = "B";
break;
default:
$target_cat1 = '';
$target_cat1_name = '';
}
You must be prepared for different values for $urlcomecatid as well.
If your $urlcomecatid variable contains neither 95 nor 96, PHP will ignore both cases, so the $target_cat1 and $target_cat1_name variables won't be initialized.
You can use default to tell PHP what to do when all cases are ignored.
http://php.net/manual/fr/control-structures.switch.php

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.

switch case isset triggered when case is -not- set

--Let me add this. This code works for me the way it is. I just do not know why it works.--
I can't figure this out.
switch ($_SERVER['QUERY_STRING']) {
case isset($_GET['test0']):
echo "test0<br>";
break;
case isset($_GET['test1']):
echo "test1<br>";
break;
case isset($_GET['test2']):
echo "test2<br>";
break;
case isset($_GET['test3']):
echo "test3<br>";
break;
case isset($_GET['test4']):
echo "test4<br>";
break;
default:
echo "no test<br>";
break;
}
When the url is index.php?test0, "test0" is shown.
When the url is index.php?test4, "test4" is shown.
When the url is index.php?test999, "no test" is shown.
When the url is index.php?tes, "no test" is shown.
When the url is index.php?, or index.php, "test0" is shown.
Why is this? The condition is not met, so should the default not be shown?
switch can't be used this way. isset() returns true or false, not something (a string, an int, etc) you can match against. What you are basically doing is:
switch ($_SERVER['QUERY_STRING']) {
case true:
echo "test0<br>";
break;
case true:
echo "test1<br>";
break;
case false:
echo "test2<br>";
break;
case false:
echo "test3<br>";
break;
case true:
echo "test4<br>";
break;
default:
echo "no test<br>";
break;
}
cases are considered from top to bottom. In this case, $_SERVER["QUERY_STRING"] is automatically type-converted to bool (which will return true in this case). The first case it sees would be test0, so it echos that. If you do that for test0-4, it will give you the false illusion that this code is working as intended, while it's not considering the edge cases.
The only way you can achieve what you want is by using multiple ifs, or by redesigning your application.
When the url is index.php?, or index.php, "test0" is shown.
Why is this? The condition is not met, so should the default not be shown?
Like a good question, your question as well contains the answer already.
You already have realized that the condition must be met even you think it is not met. Therefore you ask. So let's see which condition is met:
case isset($_GET['test0']):
echo "test0<br>";
break;
This is a test for isset($_GET['test0']) and we know with the request that this is FALSE. So this test tests for FALSE.
Now let's see against what this tests:
switch ($_SERVER['QUERY_STRING']) {
That is $_SERVER['QUERY_STRING']. So if $_SERVER['QUERY_STRING'] is FALSE the test0 will be output.
Because switch { case:} in PHP does loose comparison, the empty string $_SERVER['QUERY_STRING'] is FALSE. This is why you see the output.
Easy if you know why, right? And all so logical.
And what you wanted to test against was not $_SERVER['QUERY_STRING'] but just TRUE:
switch (TRUE)
{
case isset($_GET['test0']) :
...
}
This gets the job done, too.
<?php
$q = $_SERVER['QUERY_STRING'];
if(!empty($q) && isset($q) && strlen($q) >0 ){
$url = $q;
switch ($url){
case true;
echo $url;
break;
}
}
else {
echo "no test<br>";
}
what about
$found = false;
for($i=0;$i <=4; $i++){
if( isset($_GET['test'.$i]) ){
echo "test".$i;
$found = true;
}
}
if(!$found){
echo "no test";
}

PHP: Breaks in default case switches?

switch ($var) {
case 0:
// Do something...
break;
case 1:
// Do something...
break;
default:
// Do something...
break;
}
I've seen some people use break at the end of the default case. Since the default case is the last case that's executed when triggered, is there any need to have a break there? I'm guessing it's just done out of common practice or is there another reason?
There's no reason its required so long as the default is at the end of the switch statement. Note that the default doesn't need to be the last case: http://codepad.viper-7.com/BISiiD
<?php
$var = 4;
switch($var)
{
default:
echo "default";
break;
case 4:
echo "this will be executed";
break;
}

How do i get this or correct this PHP logic Working

I am creating a report with the data which calls the stored procedure and that procedure returns
various sections (1,2,3,4,5,6) with the data in each section.Now the sections may contain or may
not contain the data.This is how i have wriiten my logic
foreach($this->$dbresults as $row){
$var1 ='';
If($var1!=$row['section']){
switch($row['section']){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
}
}
$var1=$row['section']
}
So here My problem if any one of the section is not present then that section case cannot be executed
.I mean How do i execute the section even if the section is not returned from the database
I guess you're already ordering your results by section. If your sections are really 1-n, you could put your switch() code into some runsections function and do this:
$var1=0; $lastsection=16;
foreach($this->dbresults as $row) {
If($var1!=$row['section']){
for($num=$var1+1; $num<$row['section']; $num++) runsections($num);
runsections($row['section']);
}
$var1=$row['section'];
}
for($num=$var1+1;$num<=$lastsection;$num++) runsections($num);
if your sections aren't sequential numbers you could create an array and check if they've all been executed
$sections=array('a'=>0,'b'=>0,'c'=>0,'d'=>0,'e'=>0);
If($var1!=$row['section']){
unset($sections[$row['section']]);
runsection($row['section']);
}
...
}
foreach($sections as $num) {
runsection($num);
}
edit: so the runsections() function would look like this:
function runsections($section) {
switch($section){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
}
}
switch($x){
case '1':
echo "some thing 1";
break;
case '2':
echo "some thing 2";
break;
case 'N':
echo "some thing N";
break;
default:
echo "some thing else";
}
After your last case, insert:
default: echo "some error";
The break is optional since it's the last case in the switch statement. Also, The single quotes are also optional if you're looking for numeric options.
case 1: echo "something";
break;
Maybe I am not quite understanding exactly what you want. The following code should work in the following conditions:
You always want to display the 6
sections with either DB data or
non-DB data.
You do not need to display the same
section multiple times.
$sections = range(1, 6);
foreach($sections as $sectionNum) {
$sectionNum = (string) $sectionNum;
$foundSection = false;
foreach($this->dbresults as $row) {
if ($row['section'] == $sectionNum) {
echo "section #$sectionNum has DB data: " . $row['data'];
$foundSection = true;
break;
}
}
if (!$foundSection) {
echo "section #$sectionNum does not have DB data.";
}
}
Here's what I've got:
foreach($this->dbresults as $row){
if(isset($row['section'])){
switch($row['section']){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
default:echo "some thing data";
break;
}
} else {
//Do something since no section data was stored
}
}
I added a default case, fixed the small php errors ($this->$dbresults is changed, using isset instead of !='') and added an else to your if to do something if the section isn't found.

Categories