I have this if statement but how can I add more conditions to it?:
if($row['Type']==$rtype){
}
I would like to have two conditions so when $row['Type']=A and $rtype=B and vise versa all others should be match exact but in case when Type A and B comes together I want to allow the statement display result.
I tried by adding:
($row['Type']=='A' && $rtype=='B') || ($row['Type']=='B' && $rtype=='A')
as:
if(...&& (($row['Type']==$rtype) || ($row['Type']=='A' && $rtype=='B') || ($row['Type']=='A' && $rtype=='B'))) {
}
What is the best way to approach this?
From your comment above, it sounds like you may need more clearly define when to "show" and when to "not show". Typically, if there are only two scenarios — either show or don't show — then you wouldn't need two if conditions.
However, with what you describe above, this should work:
if (($rtype == 'A' && $row['Type'] == 'B') || ($rtype == 'B' && $row['Type'] == 'A')) {
return true;
}
if (($rtype == 'D' && $row['Type'] == 'D') || ($rtype == 'E' && $row['Type'] == 'E') || ($rtype == 'F' && $row['Type'] == 'F') {
return false;
}
Note that each group is contained within its own ( and ). That does the AND you're looking for. Outside of each group, the || will do the OR.
If you know that you don't want to "show" if the first if doesn't evaluate to true, then you can simply do:
if (($rtype == 'A' && $row['Type'] == 'B') || ($rtype == 'B' && $row['Type'] == 'A')) {
return true;
}
return false;
I'm not sure what "show" and "don't show" mean so I'm just assuming a true/false will do for you.
Also check out the Operator Precedence link that abhishek posted above. That might give you some more general knowledge about how to group your logical expressions.
Related
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'm wondering if there is any way to make this code shorter. I'm using 2 if statements and I'm looking to only use one. The things is $user is the session and if you check if $user->userId exists on the same line, the code will error when no session exists. Caused by requesting the userId from an object that does not exist. That's pretty logical but now is there any solution?
if ($user != null) {
if ($user->userId == 1) {
..
}
}
How about using the && operator:
if ($user && $user->userId == 1) {
//...
}
You can add as many sentences as you want, as long as they are properly built, in this case:
if (($user != null) && ($user->userId == 1)) {
or you could simply:
if ($user && ($user->userId == 1)) {
if ($user) just checks if the variable is set, or if it is not null.
You want to use the && operator. It means and
if ($user && $user->userId == 1) {
// do some things
}
You may also want to look into the || operator, it means or.
The && operator will return true ONLY if the two predicates return true.
The || operator will return true as long as one of the predicates return true.
I'm looking for something like this:
<?php
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'a'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'b'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'a'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'b'){
//echoing some HEREDOCS here
}
?>
I have some 7 conditions and dozens of combinations. I want to know is this the right way to address each unique type of user or there can be some better approach?
HEREDOCS for every single combination is totally different from others.
A switch with concatenated key may be the answer for you:
switch ($condition1.":".$condition2.":".$condition3){
case "a:a:a":
//echoing some HEREDOCS here
break;
case "a:a:b":
//echoing some HEREDOCS here
break;
case "a:b:a":
//echoing some HEREDOCS here
break;
case "a:b:b":
//echoing some HEREDOCS here
break;
}
Or alternatively you could create a message map
$messages = array(
"a:a:a"=>"Docs a:a:a",
"a:a:b"=>"Docs a:a:b",
"a:b:a"=>"Docs a:b:a",
"a:b:b"=>"Docs a:b:b",
);
echo $messages[$condition1.":".$condition2.":".$condition3];
You might be looking for the switch operator.
As far as your design is concerned, you might want to have each persons text stored in the database and pull it up with the user!
I always like to design my code to avoid this kind of situation. Try to define a linear list of situations first, like:
<?php
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'a') $user_type = 1;
elseif($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'b') $user_type = 2;
elseif($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'a') $user_type = 3;
elseif($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'b') $user_type = 4;
?>
Just don't forget to cover every possible combination, otherwise all ifs may fail, making $user_type undefined.
Later, when you want to print something, or take any action based on user type, just do a switch / case:
switch($user_type)
{
case 1:
//echoing some HEREDOCS here
break;
case 2:
//echoing some HEREDOCS here
break;
}
The general idea is to handle the complicated thing only once, to avoid duplicating your complex logic all around your code.
Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition
Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.
your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.
You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.
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.