how to use AND and OR together in php - php

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']))

Related

Checking Multiple $_GET variables if equals a string

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';

Compare two json file with empty value

I have two json file, and need to sync them, if changes found, below is my json file,
is parent(P) and
another one
is child(C),
Here is my code :
foreach ($localJson->PushNotification as $key => $data ) {
foreach ($this->news as $news ) {
if($news->pushMessageId == $data->pushMessageId){
if($news->Image != "" && $news->Image != $data->Image){
echo "new image found <br />";
}
if($news->documentFile != "" && $news->documentFile != $data->documentFile){
echo "new documentFile found <br />";
}
if($news->categoryIcon != "" && $news->categoryIcon != $data->categoryIcon){
echo "new categoryIcon found <br />";
}
}
}
}
The Problem is everytime when it loops, it doesent discard previous loops value, if current found to be empty
For Ex: 1st loop 1653 pushMessageId compares pdf and updates in child, but for next pushMessageId again it takes same pdf to compare, rather then taking empty field.
I have check your code , its look a simple logical condition and need to update current objects new value.
foreach ($localJson->PushNotification as $key => $data ) {
foreach ($this->news as $news ) {
if($news->pushMessageId == $data->pushMessageId){
if($news->Image != "" && $news->Image != $data->Image){
$data->Image = $news->Image;
}
if($news->documentFile != "" && $news->documentFile != $data->documentFile){
$data->documentFile = $news->documentFile;
}
if($news->categoryIcon != "" && $news->categoryIcon != $data->categoryIcon){
$data->categoryIcon = $news->categoryIcon;
}
}
}
}
I hope this solution may work for you.

If either of two cookies exist do something, in PHP

I know that || or && need to be used but I can't work out the correct or best way to format this.
My code for one cookie:
if(isset($_COOKIE['mycookie'])) {
if($_COOKIE['mycookie']=="value1") {
// do some stuff
}
}
But I'd like to include another cookie in this routine where either one can be true for the "stuff" to work.
I'm just not sure how to format this. Is it something like this?
if(isset($_COOKIE['mycookie'] || ['mycookie2')) {
if($_COOKIE['mycookie']=="value1" || $COOKIE['mycookie2']=="value2") {
// do some stuff
}
}
You can write all in one if statement if you want like this:
(The OR statement in the isset() function is not going to work)
if ( (isset($_COOKIE['mycookie']) && $_COOKIE['mycookie'] == "value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") )
You need to do the || outside the function, to combine the results of all the calls.
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
// do some stuff
}
It will be:
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
if ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2") {
// do some stuff
}
}
Or even:
if ((isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2') && ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2")) {
// do some stuff
}
to avoid nested if.
Try this
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1")
|| 9isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] =="value2" )) {
// do some stuff
}
Try this. It puts all requirements in one if statement:
if( (isset($_COOKIE['mycookie'] && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2']=="value2") ) {
// do some stuff
}
You can use one if condition instead of nested if. If you required to validate both then
if(isset($_COOKIE['mycookie'], $_COOKIE['mycookie2']) && ($_COOKIE['mycookie'] == "value1" && $_COOKIE['mycookie2']=="value2")) {
// do some stuff
}
Or if you have to validate one of them then
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") ) {
// do some stuff
}

php array empty - how to get NOT

if($_POST['cropProfileData'] == undefined || $_POST['cropProfileData'] == null || $_POST['cropProfileData'] == 0) {
echo "NO process profile photo....";
}else{
echo "YES process profile photo....";
}
this works fine - it shows me if the array is empty.
The catch is I only need the 'echo "YES process profile photo...".
How can I get the NOT of the above IF statement? I've tried added '!' after the if - eg: if!( and I've tried added '!' after each of the 3 statements.
Is there a way to get the NOT of this statement - will save me a having to include the else which is pointless code.
thx
PS: the reason for this is to see if the array is empty.
Just !empty( $_POST[ 'cropProfileData' ] ) will cover all those cases (although undefined is not used in PHP unless it's some constant).
if( !empty( $_POST[ 'cropProfileData' ] ) ) {
echo "YES process profile photo....";
}
Theory
The negative expression is simply this:
if (!($_POST['cropProfileData'] == undefined || $_POST['cropProfileData'] == null || $_POST['cropProfileData'] == 0)) {
According to De Morgan, the expression could also be written as:
if ($_POST['cropProfileData'] != undefined && $_POST['cropProfileData'] != null && $_POST['cropProfileData'] != 0) {
Practice
To test if something is undefined you should use isset(), for example:
$_POST['cropProfileData'] == undefined || $_POST['cropProfileData'] == null
Should be written as:
!isset($_POST['cropProfileData'])
To specifically test if an array is not empty:
isset($_POST['cropProfileData']) && is_array($_POST['cropProfileData']) && count($_POST['cropProfileData'])
Or, simpler:
if (!empty($_POST['cropProfileData']) && is_array($_POST['cropProfileData'])) {
<?php
/*
if($a == undefined || $b == null || $c == 0) {
echo "NO process profile photo....";
}else{
echo "YES process profile photo....";
}
*/
if($a != undefined && $b != null && $c != 0) {
echo "NO process profile photo....";
}else{
echo "YES process profile photo....";
}
?>
You just need to check that array is empty or not
like
if(!empty( $_POST[ 'cropProfileData' ] )){echo "YES process profile photo....";}

How do I extract a single column from another database?

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

Categories