Variable scope in php in 2 block codes - php

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

Related

Unexpected results using dynamic variables

The following code using switch and dynamic variables should return "b1" but it returns "11".
Is this a bug or am I doing something wrong?
<?php
$d = "Tuesday";
switch($d) {
case "Monday":
$$previousdayofmonthrow = "a";
$$previousdayofmonthcol = "7";
break;
case "Tuesday":
$$previousdayofmonthrow = "b";
$$previousdayofmonthcol = "1";
break;
case "Wednesday":
$$previousdayofmonthrow = "b";
$$previousdayofmonthcol = "2";
break;
case "Thursday":
$$previousdayofmonthrow = "b";
$$previousdayofmonthcol = "3";
break;
case "Friday":
$$previousdayofmonthrow = "b";
$$previousdayofmonthcol = "4";
break;
case "Saturday":
$$previousdayofmonthrow = "b";
$$previousdayofmonthcol = "5";
break;
case "Sunday":
$$previousdayofmonthrow = "b";
$$previousdayofmonthcol = "6";
break;
}
echo $$previousdayofmonthrow;
echo $$previousdayofmonthcol;
?>
Live example > http://codepad.org/wNfCqffD
tldr; It is not a bug in PHP related to dynamic variables, nor is it related with the switch statement.
The behavior of the test-case is correct and is well-defined, even if not expected.
This is because both $previousdayofmonthrow and $previousdayofmonthcol evaluate to undefined (did have notices enabled, no?) and thus both "dynamic variables" (aka variable-variable) expressions operate on the same variable.
Here is a minimal reproduction of the the behavior, without a switch, that also shows some interesting intermediate values:
$x = undefined; // The original doesn't set a value; it is implicitly undefined
$y = undefined; // but the effect is the same, and this way avoids warnings - yay!
$$x = "a";
echo $$x; // -> "a"
echo $$y; // -> "a"
$$y = "b";
echo $$x; // -> "b"
echo $$y; // -> "b"
This "linked" behavior occurs because, as previously stated, the variable-variable expression access the same variable - mainly the variable called "undefined". (The value of the expression used as the dynamic variable name is turned into a string and "" . undefined -> "undefined"):
echo ${"undefined"}; // -> "b"
This "assignment of undefined" is allowed because undefined in PHP is a reserved word - and not a constant/variable. Thus it is not prohibited to use "undefined" as a variable name even though it cannot appear as an unquoted identifier.
FWIW: Consider not using variable-variables; it is almost always better to use a discrete array when such "dynamic keys" are required.

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.

Calling PHP functions through another file

I created a simple sample php file for display some function outputs. here is the code..
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('Joel');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('Duck');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
function hello($name){
return $name;
}
function pageHeader($header,$onoff){
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
echo $printHeader;
echo $printName;
?>
This code is working fine without any problems.
When I call 'example.com/php/tipo34.php?red', it shows on the screen:
redpage header
Joel
And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:
Duck
I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:
Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8
The code I tried:
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
include 'tipo34-req.php';
echo $printHeader;
echo $printName;
?>
tipo34-req.php code:
<?php
function hello($name){
global $name;
return $name;
}
function pageHeader($header,$onoff){ global $header, $onoff
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
?>
How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.
Thanks.
Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.
include 'tipo34-req.php';
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
echo $printHeader;
echo $printName;
?>
Have you tried including the file at the top before calling any of the functions?

Check several field to execute switch case

I have a code to check through several fields to execute certain code as below:
switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
But it does not work.
Anyone can help?
Switch support one value only. Only IF condition can have OR and AND condition.
$tags = get the tag value.
switch($tags){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
The following should do the trick for you:
<?php
$tag1='satay';
$tag2='test2';
$tag3='digitalmarketing';
function isItThere($myTag)
{
switch($myTag)
{
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
echo $imgput;
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
echo $imgput;
break;
// etc etc
}
}
for($i=1;$i<4;$i++)
{
isItThere(${'tag'.$i});
}
?>
I have basically set up a small function that contains the switch statement and written a simple loop to test the variables.
As I said in my comment, you can't use more than one variable in the switch statement, but this will provide you a nice clean workaround to do the same thing without the need to write many long statements.

PHP Switch statement error

<?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.

Categories