Should I always use isset() for URL arguments? - php

Here is my code:
$order_newest = $order_votes = $order_featured = $order_frequent = '';
switch ($_GET['o']) {
case 'newest':
$order_newest = 'order_active';
break;
case 'votes':
$order_votes = 'order_active';
break;
case 'featured':
$order_featured = 'order_active';
break;
case 'frequent':
$order_frequent = 'order_active';
break;
default:
$order_newest = 'order_active';
break;
}
It throws following error when o argument isn't exist in the URL:
Notice: Undefined index: o in C:\xampp\htdocs... on line 71
I can fix the problem by adding this condition:
$order_newest = $order_votes = $order_featured = $order_frequent = '';
if ( isset($_GET['o']) ) {
switch ($_GET['o']) {
.
.
.
}
} else {
$order_newest = 'order_active';
}
But I guess this isn't the right way. Because in this case I have to add lots of conditions contain isset() function.
Anyway, is there any better approach to handle that?

Use filter_input(). As example:
$order = filter_input(INPUT_GET, 'o');
switch ($order) {
// ...

Related

Reference expression in switch

In PHP, is it possible to get a reference to the expression used in the switch statement?
For example:
switch ($_POST['id']) {
case 0:
$id = <switch expression>
break;
case 1:
$id = <switch expression>
break;
default
$id = null;
}
so if $_POST['id'] = 1, then $id = 1
Then I can test for if (! $id) {}
Obviously you are probably thinking why not just use $id = $_POST['id'] but in the real example it looks like this
switch (strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = <switch/case expression>;
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = <switch/case expression>;
$payment_type = 'ecurrency';
break;
default:
//$payment_method = null; // taken from card number
$payment_type = 'card';
}
I dont want $payment_method assigned.
HAD A EUREKA MOMENT WHILST WRITING THIS
Well, that works for what I was trying to achieve anyway.
switch (($payment_method = strtolower($load->post('payment_method'))))
{
case 'paypal':
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_type = 'ecurrency';
break;
default:
unset($payment_method); // taken from card number
$payment_type = 'card';
}
There are no way
use for example such way
$cases = array(0, 1, 3 ,5);
$defaultVal = 1;
$id = in_array($_POST['id'], $cases) ? $_POST['id']: $defaultVal;
AFAIK there is no such feature in PHP.
But you can do what you are looking for as easy as this:
switch (strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = 'paypal';
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = 'bitcoin';
$payment_type = 'ecurrency';
break;
default:
$payment_method = null; // taken from card type
$payment_type = 'card';
}
Actually I just realize that this probably is possible using a simple workaround:
switch ($switch_value = strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = $switch_value;
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = $switch_value;
$payment_type = 'ecurrency';
break;
default:
$payment_method = null; // taken from card type
$payment_type = 'card';
}
;-)

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.

Using conditional values from an array in an if...statement

I have an array of conditions :
$arrConditions = array ('>=2', '==1', '<=10');
...which I want to be able to use in an if...statement.
IE.
if (5 $arrConditions[0])
{
...do something
}
...which would be the same as :
if (5 >= 2)
{
...do something
}
Any help?
Thanks
Such a requirement is a sure sign of a bad design.
Most likely you can do that another, more usual way.
Nevertheless, never use eval for such things.
At least store each operator in pairs - an operator and operand.
$arrConditions = array (
array('>=',2),
array('==',1),
array('<=',10),
);
and then use switch:
list ($operator,$operand) = $arrConditions[0];
switch($operator) {
case '==':
$result = ($input == $operand);
break;
case '>=':
$result = ($input >= $operand);
break;
// and so on
}
But again - most likely you can solve it another, much easier way.
What about this ?
<?php
$arrConditions = array('==2', '==9', '==5', '==1', '==10', '==6', '==7');
$count = 0;
$myval = 0;
foreach ($arrConditions as $cond) {
$str = "if(5 $cond) { return $count;}";
$evalval = eval($str);
if (!empty($evalval)) {
$myval = $count;
}
$count++;
}
switch ($myval) {
case 0: echo '==2 satisfied';
break;
case 1: echo '==9 satisfied';
break;
case 2: echo '==5 satisfied';
break;
case 3: echo '==1 satisfied';
break;
case 4: echo '==10 satisfied';
break;
default : echo 'No condition satisfied';
}
?>

Categories