if condition 1 :
if(isset($_POST['searchOrder']) && $_POST['searchOrder']!='')
{
$userToSearch = "WHERE name='Conformed'";
}
If condition 2 :
if(strstr($status, "value1") !== false )
{
$hide .= 'style="display: none;"';
}
i need if condition 1 OR If condition 2 , i tried below :
if (((strstr($status, "value1") !== false) ||
isset($_POST['searchOrder']) && $_POST['searchOrder']!='')
{
$userToSearch = "WHERE name='Conformed'";
})
{
$hide .= 'style="display: none;"';
}
I also tried below , but both gave error....
if ((strstr($status, "value1") !== false) || isset($_POST['searchOrder']) && $_POST['searchOrder']!=''))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
&& has higher precedence than ||, so you might want to rearrange your conditions, or put parentheses around the second group. Then just write the second part in the same block, like this.
if (strstr($status, "value1") !== false || (isset($_POST['searchOrder']) && $_POST['searchOrder']!=''))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
Something like that?
if((isset($_POST['searchOrder']) && $_POST['searchOrder']!='') || strstr($status, "value1") !== false) {
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
If you need to satisfy both conditions then do something like this :
if (((strstr($status, "value1") !== false)) && (isset($_POST['searchOrder']) && $_POST['searchOrder']!='')))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
If you want two conditions alternatively you should remember about grouping.
(condition1) || (condition2)
In your case you've got && condition as part of second case, it is not grouped so it is processing first your OR condition between condition1 and first part of condition2, than last AND which is not you wanted.
Related
Am trying to write a simple PHP function that uses both && and || function in php but am only able to use the && function, i get a desired result as shown with the below code
<?php
$srt = "asc";
if($srt != "" && $srt != "asc"){
echo "close";
} else {
echo "open";
}
?>
output >> open
But when i add the || function to it i get the wrong result
<?php
$srt = "asc";
if($srt != "" && $srt != "asc" || $srt != "desc"){
echo "close";
} else {
echo "open";
}
?>
output >> close
I have also try using AND in place of && and OR in place of || since php accepts both but i still get the same result am new to php so i dont know if what am trying is allowed or not so any advise will be greatly appreciated.
Use extra parenthesis to make it an expression.
if ($srt != "" && ($srt != "asc" || $srt != "desc"))
So you compare $srt != "" AND $srt != "asc" || $srt != "desc".
Your code can be improved for readability as
if (!in_array($srt, ['', 'asc', 'desc']))
I have done an email activation, which is working but am trying to adopt the same concept for my antispam activation. It is working for some people but sometimes, I keep getting that one of the variables is not set, is just just a trial and error thing?
I also read somewhere that I should include a not empty check too after the isset() check.
<?php
include_once __DIR__.'/header2.php';
if(isset($_SESSION['u_uid'])) {
echo "<meta http-equiv='refresh' content='0;url=../header2.php?reply=mustloggedoutfirst'>";
exit();
} else {
if(!isset($_GET['email']) && $_GET['email'] !== '' || !isset($_GET['userid']) && $_GET['userid'] !== '' || !isset($_GET['id']) && $_GET['id'] !== '' || !isset($_GET['reply_registration_expirydate']) && $_GET['reply_registration_expirydate'] !== '') {
echo "<meta http-equiv='refresh' content='0;url=../index.php?reply=missinglink'>";
exit();
} else {
$email = strip_tags($_GET['email']);
$username = strip_tags($_GET['userid']);
$id = strip_tags($_GET['id']);
$reply_registration_expirydate = strip_tags($_GET['reply_registration_expirydate']);
if (empty($_SESSION['key'])) {
$_SESSION['key'] = base64_encode(openssl_random_pseudo_bytes(32));
}
}
}
I forgot to add to my previous question that initially, I had the following code, which seems to work for most people for not for some...
if (!isset($_GET['email']) || !isset($_GET['activatetoken']) || !isset($_GET['reply_registration_expirydate'])) {
echo "<meta http-equiv='refresh' content='0;url=../index.php?reply=missinglink'>";
exit();
Update your IF-clause and remove the negation of "isset".
With "!isset($_GET['email']) && $_GET['email'] !== ''" you're checking if the variable is NOT set and then (being not set) if it's NOT unequal to an empty string, this must run into a "variable not set"-error.
The logical order of your if statement is probably incorrect. By "missing link errors" you probably mean this part of the if-statement is executed:
if(!isset($_GET['email']) && $_GET['email'] !== '' || !isset($_GET['userid']) && $_GET['userid'] !== '' || !isset($_GET['id']) && $_GET['id'] !== '' || !isset($_GET['reply_registration_expirydate']) && $_GET['reply_registration_expirydate'] !== '') {
formatted:
if (! isset($_GET['email']) // IS NOT SET email
&& $_GET['email'] !== '' // and IS NOT EMPTY email
|| !isset($_GET['userid']) // or IS NOT SET userid
&& $_GET['userid'] !== '' // and IS NOT EMPTY userid
|| !isset($_GET['id']) // or IS NOT SET id
&& $_GET['id'] !== '' // and IS NOT EMPTY id
|| !isset($_GET['reply_registration_expirydate']) // or IS NOT SET reply_registration_expirydate
&& $_GET['reply_registration_expirydate'] !== '') { // and IS NOT EMPTY reply_registration_expirydate
Due to the precedence of the operators, your statement might evaluate to true earlier than you want. You need to use parentheses to group operands.
if ((! isset($_GET['email']) // (IS NOT SET email
&& $_GET['email'] !== '') // and IS NOT EMPTY email)
|| (!isset($_GET['userid']) // or (IS NOT SET userid
&& $_GET['userid'] !== '') // and IS NOT EMPTY userid)
|| (!isset($_GET['id']) // or (IS NOT SET id
&& $_GET['id'] !== '') // and IS NOT EMPTY id)
|| (!isset($_GET['reply_registration_expirydate']) // or (IS NOT SET reply_registration_expirydate
&& $_GET['reply_registration_expirydate'] !== '')) { // and IS NOT EMPTY reply_registration_expirydate)
if(isset($_GET['a']) || isset($_GET['b']) || isset($_GET['c'])){
if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x"){
echo "YES";
} else {
echo "NO";
}
}
in this php code, i'm trying to check if one of those requests isset and if one of them value == 'x' or not, But the 2nd part if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x") doesn't work as intended at all, I wrapped it inside () hoping it would work, In this condition, do i have to separate it as i did inthe isset() part? or is there a better method to do that?
This is likely what you are looking for
UPDATE - I just changed || to && for the last condition in case you were quick to try it out.
if( (isset($_GET['a']) && $_GET['a'] == "x") || (isset($_GET['b']) && $_GET['b'] == "x") || (isset($_GET['c']) && $_GET['c'] == "x")){
echo "YES";
} else {
echo "NO";
}
If you have to write a lot of conditionals you could use one of the following:
Using a foreach and a conditional:
$either_abc_is_x = function() {
$keys = ['a','b','c'];
foreach($keys as $key)
if(isset($_GET[$key]) && $_GET[$key] == "x")
return true;
return false;
};
echo $either_abc_is_x() ? 'YES' : 'NO';
Using a an array filter with a conditional:
$get_abc_keys_equal_to_x = array_filter(['a','b','c'], function($v) {
return isset($_GET[$v]) && $_GET[$v] == 'x';
});
echo $get_abc_keys_equal_to_x ? 'YES' : 'NO';
Array gymnastics:
$either_abc_is_x = isset($_GET) && in_array('x', array_intersect_key($_GET, array_flip(['a','b','c'])));
echo $either_abc_is_x ? 'YES' : 'NO';
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'.
My SQL query below won't output the $srchRegion && $srchCategory string if there are $_POST values for both. Why?
if (($srchRegion!='00') || ($srchRegion!='') && ($srchCategory=='00') || ($srchCategory=='')) {
return $query .= "AND region='$srchRegion'";
} elseif (($srchRegion=='00') || ($srchRegion=='') && ($srchCategory!='00') || ($srchCategory!='')) {
return $query .= "AND category='$srchCategory'";
} elseif (($srchRegion!='00') || ($srchRegion!='') && ($srchCategory!='00') || ($srchCategory!='')) {
return $query .= "AND region='$srchRegion' AND category='$srchCategory'";
}
Is there a better way of doing this?
Instead of looking for all possible boolean values and cluttered if-else, why just keep on appending the where clause based on corresponding value populated.
if($srchRegion != '00' && $srchRegion != '')
$query .= " AND region='$srchRegion'";
if($srchCategory != '00' && $srchCategory != '')
$query .= " AND category='$srchCategory'";
return $query;
So $query acts as query builder and it keeps on adding where clause based on $_POST values being set.
Try this:
if(($srchRegion!='00' || $srchRegion!='') && ($srchCategory=='00' || $srchCategory=='')){
return $query .= "AND region='$srchRegion'";
} elseif(($srchRegion=='00' || $srchRegion=='') && ($srchCategory!='00' || $srchCategory!='')){
return $query .= "AND category='$srchCategory'";
} elseif($srchRegion!='00' || $srchRegion!='') && ($srchCategory!='00' || $srchCategory!='')){
return $query .= "AND region='$srchRegion' AND category='$srchCategory'";
}
You haven't structured the if condition right. You check two OR conditions with one AND condition, so you'll need to put the OR conditions in brackets.