I'm trying to make routing system.
What I want to do:
Url: http://localhost/wiki.php?post_id=1 invoke second if
Url's: http://localhost/wiki.php?post_id=1&action=upvote
http://localhost/wiki.php?post_id=1&action=downvote invoke first if
This is routing system:
if(isset($_GET['post_id']) & ((isset($_GET['action']) ? $_GET['action'] : null) == "upvote" || "downvote")){
//Do stuff
}else if(isset($_GET['post_id'])){
//Do stuff
}
Problem:
Url's: http://localhost/wiki.php?post_id=1&action=upvote http://localhost/wiki.php?post_id=1&action=downvote work like they are supposed to.
But url: http://localhost/wiki.php?post_id=1 invokes first not second if too...
Notice: Undefined index: action in D:\xampp\htdocs\controllers\WikiController.php on line 18
Line 18: $rate = $_GET['action']; it's inside in first if.
While testing I figured out that if I change if(isset($_GET['post_id']) & ((isset($_GET['action']) ? $_GET['action'] : null) == "upvote" || "downvote")){
too
if(isset($_GET['post_id']) & ((isset($_GET['action']) ? $_GET['action'] : null) == "upvote")){
or
if(isset($_GET['post_id']) & ((isset($_GET['action']) ? $_GET['action'] : null) == "downvote")){
It works fine. But I want to learn how to do this right, not just simply made 2 different routers.
this part (exp) == "upvote" || "downvote" does not work like you suppose it does. It only tests if exp is equal to "upvote", the second part "downvote" is understood as a boolean which is true, so you stay in the first part of the alternative..
try with
if(isset($_GET['post_id']) && (($exp = (isset($_GET['action']) ? $_GET['action'] : null)) == "upvote" || $exp == "downvote")){
For clarification, a single (ampersand) & is "bitwise":
http://php.net/manual/en/language.operators.bitwise.php
Therefore, you need to change your & to &&.
From the manual:
$a & $b And Bits that are set in both $a and $b are set.
What you're after is the logical && operator:
http://php.net/manual/en/language.operators.logical.php
$a && $b And TRUE if both $a and $b are TRUE.
|| "downvote" will always return true since you're not comparing it to anything. You have to add (isset($_GET['action']) ? $_GET['action'] : null) == in front of that string too.
So your statement would be:
if(isset($_GET['post_id']) && ((isset($_GET['action']) ? $_GET['action'] : null) == "upvote" || (isset($_GET['action']) ? $_GET['action'] : null) == "downvote")){
//Do stuff
}else if(isset($_GET['post_id'])){
//Do stuff
}
$routes = [
'upvote','downvote'
];
if(isset($_GET['post_id']) && isset($_GET['action']) && in_array($_GET['action'],$routes)){
echo 'running if';
}else if(isset($_GET['post_id'])){
echo 'running else if';
}
You are missing && was the problem, i just change this for dynamic route if you need to change in future!
Related
I have been making stuff in PHP for a while, everything is always fine. But today I don't get this statement. Why is it always true?
if ($action != 1 || $action != 2) echo true; // TRUE for 0, 1, 2, 3
But the reverse logic
if ($action == 1 || $action == 2) echo true; // FALSE for 0, 3 TRUE for 1, 2
The first expression blows my mind. I guess I don't understand something very very basic, not in PHP but in the Universe, so I don't get it here. I thought that if (FALSE || TRUE) == FALSE, but it isn't a case for second example. It works as expected.
So, where is the answer how to say that: "If the variable is not 1 OR 2 - echo true". I don't understand why my if ($var != 1 OR $var != 2) echo true; doesn't work as I expect.
Negation of ($action != 1 || $action != 2) is ($action == 1 && $action == 2). You can see for yourself that the latter is always false because variable can not be both 1 and 2 at the same time. Therefore the original condition is bound to be always true.
It is working as it has to work. See the doc http://php.net/manual/en/language.operators.logical.php
$a || $b returns TRUE if either $a or $b is TRUE.
If you try like this, hope it will make sense, see the comment on every line
$action = 0;
var_dump($action != 1 || $action != 2); //here (true || true)
$action = 1;
var_dump($action != 1 || $action != 2); //here (false || true)
$action = 2;
var_dump($action != 1 || $action != 2); //here (true || false)
$action = 3;
var_dump($action != 1 || $action != 2); //here (true || true)
Condition OR will search for first TRUE result, your code will give TRUE all the time as any value will be even not 1 or 2.
From you example if $action = 1 then the condition $action != 2 will give true, also if $action = 2 then $action != 1 will give TRUE.
For (If the variable is not 1 OR 2 - echo true) use this:
if(!in_array($action, array(1,2)) echo "true";
EDIT:
You can also check it like this:
if(!($action == 1 || $action == 2)) echo "true";
if ($action == 2 || $action == 1) echo true; // FALSE for 0, 3 TRUE for 1, 2
Works as intended. Its basically "echo true if $action is either 1 or 2".
I have 2 URLs say
http://localhost/xyz?language=en
http://localhost/xyz?language=es
for which I want to check if language parameter has something other than en/es, then it should redirect to some http://localhost/xyz/errorpage
For this I have below code:
if(isset($_GET['language'])){
if(($_GET['language'] !== "en") || ($_GET['language'] !== "es")){
header('Location: /xyz/errorpage');
}
}
But practically when I execute the any of the 2 URLs or putting value of language parameter to something different than en/es:
http://localhost/xyz?language=en
http://localhost/xyz?language=es
http://localhost/xyz?language=esdfsdf
I am redirected to errorpage
Cannot understand the issue with code.
Replace || by &&.
The reason :
You want to redirect only if this is not en AND not es.
change the if statment to && instead of || or your condition will be always false.
if(isset($_GET['language'])){
if($_GET['language'] !== "en" && $_GET['language'] !== "es"){
header('Location: /xyz/errorpage');
}
}
You have bad condition, or better, operator.
Use && instead of ||, or in_array().
if(($_GET['language'] !== "en") && ($_GET['language'] !== "es")) {
Using in_array() function:
if (!in_array($_GET['languge'], array('en', 'es'))) {
header ();
}
Condition if ($a != 'x' || $a != 'y') is always true, first or the second part of condition has be true. There are no other ways.
I have been racking my brain for a bit and getting nowhere. I'm probably missing something really simple, but what I effectively need is.
if string 1 or 2 is set then compare with string 3 providing string 3 is not null.
if($_SESSION['code'] || $_GET['id'] <> $_POST['code'] && isset($_POST['code']))
I'm not sure I follow, but you're logic seems to state:
if(isset($string3)){
if(isset($string1)){
strcmp($string1,$string3);
}elseif(isset($string2)){
strcmp($string2,$string3);
}
}
if(isset($string3) && ((isset($string1) && $string1 == $string3) || (isset($string2) && $string2 == $string3)))
Something like this would work. First see if isset $string3. Then a wrapped or operator individually checking isset $string1 or 2 and if they equal string 3.
if( ($a = $_SESSION['code'] ? $_SESSION['code'] : $_GET['id'])
&& isset($_POST['code']) && $a == $_POST['code']) {
If you want $_GET['id'] to override $_SESSION['code'] in the case that both are set:
if( ($a = $_GET['id'] ? $_GET['id'] : $_SESSION['code'])
&& isset($_POST['code']) && $a == $_POST['code']) {
More correctly:
if( (isset($_SESSION['code']) || isset($_GET['id']))
&& ($a = isset($_GET['id']) ? $_GET['id'] : $_SESSION['code'])
&& isset($_POST['code']) && $a == $_POST['code']) {
if((!empty($_POST['code']) || !empty($_SESSION['code'])) && $_GET['id']<>$_POST['code']){
This could be
if(!empty($_SESSION['code']) && !empty($_GET['id']) && !empty($_POST['code'])
($_SESSION['code'] == $_POST['code'] || $_GET['id'] == $_POST['code']))
This means all the variables must be set and have value then it compares if the $_SESSION['code'] or $_GET['id'] is equal with the $_POST['code']
I'm creating an edit page which should get called this way:
users.php?action=edit&id=5
This is my code for this:
} elseif (isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
As you see it's long. First a check with isset is needed. I know you can leave that out, but that way I'll get PHP notices when error reporting is set to 'E_ALL'.
I can create a function to make it shorter but that way I'll need to create too many functions as I have such code on different places in my scripts, each requiring different information.
Is there any way to make this code shorter?
Thanks!
Since action and id both are probably going to be used might as well set them at the top of the script:
$action = !empty($_GET['action'])?$_GET['action']:false;
$id = !empty($_GET['id'])?$_GET['id']:false;
switch ($action) {
case 'edit':
if ($id !== false) {
//processing here
}
break;
default:
echo 'No known action was passed through';
}
The initial variable declaration uses the ternary operator which is a shortened if/else as an fyi.
Extra Information
I prefer this method as appose to insane if/elseif/else statements, given that it is much easier to read and you do not have to think about your logic nearly as much, so it would make it less prone to errors.
You could write a function that takes an array of keys:
function check_get_params($keys) {
foreach ($keys as $key) {
if (! isset($_GET[$key]) ) {
return false;
}
}
return true;
}
Then your line above would be:
} elseif (check_get_params(array('action', 'id')) && $_GET['action'] == 'edit' && is_numeric($_GET['id'])) {
which would be cleaner as:
} elseif (check_get_params(array('action', 'id'))) {
if ($_GET['action'] == 'edit' && is_numeric($_GET['id'])) {
I would check parameters first:
$action = (isset($_GET['action']) && !empty($_GET['action'])) ? $_GET['action'] : false;
$id = (isset($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : false;
settype($id, 'int'); // "123" became 123(int)
And then go with:
} elseif ($action && $id && $action == 'edit' && $id > 0) {
// here we are
}
This may be the way my server is set up, but I'm banging my head against the wall. I'm trying to say that if $action has no value or has a value that is not "add" or "delete" then have an error, else keep running the script. However, I get an error no matter what $action is.
$action = $_GET['a'];
if((!isset($action)) || ($action != "add" || $action != "delete")){
//header("location:index.php");
echo "error <br>";
}
$action is being set properly and if run something like if($action =="add") it works. This is on my local host, so it could be a settings issue.
Your logic is slightly off. The second || should be &&:
if ((!isset($action)) || ($action != "add" && $action != "delete"))
You can see why your original line fails by trying out a sample value. Let's say $action is "delete". Here's how the condition reduces down step by step:
// $action == "delete"
if ((!isset($action)) || ($action != "add" || $action != "delete"))
if ((!true) || ($action != "add" || $action != "delete"))
if (false || ($action != "add" || $action != "delete"))
if ($action != "add" || $action != "delete")
if (true || $action != "delete")
if (true || false)
if (true)
Oops! The condition just succeeded and printed "error", but it was supposed to fail. In fact, if you think about it, no matter what the value of $action is, one of the two != tests will return true. Switch the || to && and then the second to last line becomes if (true && false), which properly reduces to if (false).
There is a way to use || and have the test work, by the way. You have to negate everything else using De Morgan's law, i.e.:
if ((!isset($action)) || !($action == "add" || $action == "delete"))
You can read that in English as "if action is not (either add or remove), then".
No matter what $action is, it will always either not be "add" OR not be "delete", which is why the if condition always passes. What you want is to use && instead of ||:
(!isset($action)) || ($action !="add" && $action !="delete"))
You're saying "if it's not set or it's different from add or it's different from delete". You realize that a != x && a != y, with x != y is necessarily false since a cannot be simultaneously two different values.
You could also try:
if ((!isset($action)) || !($action == "add" || $action == "delete")) {
// Do your stuff
}
For future reference, you can quickly create a truth table to check if it evaluates the way you want... it's kind of like Sudoku.
(!isset($action)) && ($action != "add" && $action != "delete"))
Example:
column 1 is issetaction, column 2 and 3 evaluates !="add","delete" respectively
if($a=add) T && (F && T) => T && F => FALSE
if($a=delete) T && (T && F) => T && F => FALSE
if($a=nothing) T && (T && T) => T && T => TRUE
I think this is the best and easiest way to do it:
if (!(isset($action) && ($action == "add" || $action == "delete")))
Not an answer, but just for the sake of code formatting
if((isset($_GET['a'])) $action=$_GET['a']; else $action ="";
if(!($action === "add" OR $action === "delete")){
header("location: /index.php");
exit;
}
Note the exit; statement after header(). That's the important thing. header() does not terminate script execution.