How can make simpler this "IF"-condition - php

$regionFilter = isset($additionalDate['region']) &&
in_array($additionalDate['region'], $this->request["insuranceCompanyRegion"]);
$cityFilter = isset($additionalDate['city']) &&
in_array($additionalDate['city'], $this->request["insuranceCompanyCity"]);
if ($isRegionFilter && $isCityFilter) {
$filterCondition = $regionFilter && $cityFilter;
} elseif ($isCityFilter) {
$filterCondition = $cityFilter;
} elseif ($isRegionFilter) {
$filterCondition = $regionFilter;
}
if ($filterCondition) {
$this->companyIds[$q->id] = $q->name;
}
I need to make this condition simpler to edit in the future, how can I make it?

if ($regionFilter || $cityFilter) {
$this->companyIds[$q->id] = $q->name;
}

You can use shorthand If / Else check this 1
so for example
if($isRegionFilter && $isCityFilter){
$filterCondition = $regionFilter && $cityFilter;
becomes
$filterCondition=($isRegionFilter && $isCityFilter) ? $regionFilter && $cityFilter;

$filterCondition=($isRegionFilter && $isCityFilter) ? $regionFilter && $cityFilter : (($isRegionFilter) ? $regionFilter : $cityFilter);

Related

PHP: confused about if else statement - same logic or wrong understanding?

I'm really confused, because I have an if/else statement what works at a first integration in the system, but in a second case I must rewrite the function. My opinion is, that both statements has the same logic? Or isn't it?
Statement 1: works as intended in first integration of code, but not at the second integration (always the variable ba_geschaeftszeichen has a string lenght of zero):
if (
(isset($_POST['ba_geschaeftszeichen']) && ($kostentraeger == "sozialamt")) ||
(isset($_POST['pk_vnr']) && ($kostentraeger == "pflegekasse"))
) {
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0)) {
$ba_geschaeftszeichen = $_POST['ba_geschaeftszeichen'];
} else if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0)) {
$ba_geschaeftszeichen = $_POST['pk_vnr'];
} else {
$ba_geschaeftszeichen = "";
}
} else { $ba_geschaeftszeichen = ""; }
Statement 2: only this code works at the second integration:
if (
(isset($_POST['ba_geschaeftszeichen']) && ($kostentraeger == "sozialamt")) ||
(isset($_POST['pk_vnr']) && ($kostentraeger == "pflegekasse"))
) {
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0)) {
$ba_geschaeftszeichen = $_POST['ba_geschaeftszeichen'];
} else {
$ba_geschaeftszeichen = "";
}
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0)) {
$ba_geschaeftszeichen = $_POST['pk_vnr'];
} else {
$ba_geschaeftszeichen = "";
}
} else { $ba_geschaeftszeichen = ""; }
In statement 1, you reach
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0))
only if
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0))
is false.
In statement 2, you reach
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0))
regardless.

Solution for eval

I am doing custom search for table. I have three search parameters: from, to and status. I have used eval() to filter result according to received parameter. Below is my code:
$search = ($from != "" || $to != "" || $status != "" );
if ($search) {
if ($from != '') {
$condition[] = '$from == $res["from_number"]';
}
if ($to != '') {
$condition[] = '$to == $res["to_number"]';
}
if ($status != '') {
$condition[] = '$status == $log["status"]';
}
$search = "if(" . implode(' && ', $condition) . '){ return false; } else { return true; }';
}
After getting the conditions I am using eval
if (eval($search)) {
}
My problem is I don't want to use eval(). It may cause security issues. Ladder if else is not possible, it would be very lengthy. Any other solution?
e.g. If i have passed value for status then i want check like
if($status == $log["status"]) {
}
if i have passed to & from number then it should be like:
if($from == $res["from_number"] && $to == $res["to_number"]) {
}
Don't use eval - it is potentially dangerous and not recommended to use.
Your code can be like this:
$result = false;
if ($from != "" || $to != "" || $status != "") {
if ($from != '' && $from != $res["from_number"]) $result = true;
if ($to != '' && $to != $res["to_number"]) $result = true;
if ($status != '' && $status != $log["status"]) $result = true;
}
if ($result) {
// ........
}

undefined index but i can't understand why

Here is my code
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && $_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
Please help.
Thanks.
When you do && it will evaluate all conditions until something is false. When you do || it will evaluate all conditions until something is true. Since your first conditions evaluated to the false, the 2nd one was invoked but $_POST['error'] didn't exist.
You probably want to do this, notice the brackets around your two errors.
if(
isset($_POST['error']) &&
(
$_POST['error'] == 2 ||
$_POST['error'] == 1
)
)
It can also be better re-written as.
if(
isset($_POST['error']) &&
in_array($_POST['error'], array(1,2))
)
Like Augwa said:
The && operator will evaluate all conditions until any one of them is false.
The || operator will evaluate all conditions until any one of the is true.
A solution:
if(isset($_POST['error'])) {
if($_POST['error'] != 2 && $_POST['error'] !=1) {
// Do stuff here
return true;
}else if($_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
}
you should change your code as below... always enclose your comparison with || in brackets. because || condition checks up to last piece of code to find out a 'true' value. by re-coding as ..... && (... || .... ), the executions will return from the point && and will not execute ( .... || ..... ) part
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && ($_POST['error'] == 2 || $_POST['error'] == 1)) {
return false;
} else {
return false;
}

In PHP how do you do an if statement within the if of an if statement?

So the question is confusing I know. This is what I am wondering how to do.
I have the following if statement:
if(
(isset($_POST['billing_company']) && $_POST['billing_company'] != "") &&
(isset($_POST['billing_address']) && $_POST['billing_address'] != "") &&
(isset($_POST['billing_city']) && $_POST['billing_city'] != "") &&
(isset($_POST['billing_state']) && $_POST['billing_state'] != "") &&
(isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") &&
(isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") &&
(isset($_POST['location_name']) && $_POST['location_name'] != "") &&
(isset($_POST['location_address']) && $_POST['location_address'] != "") &&
(isset($_POST['location_city']) && $_POST['location_city'] != "") &&
(isset($_POST['location_state']) && $_POST['location_state'] != "") &&
(isset($_POST['location_zip']) && $_POST['location_zip'] != "") &&
(isset($_POST['location_phone']) && $_POST['location_phone'] != "") &&
(isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") &&
(isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") &&
(isset($_POST['user_username']) && $_POST['user_username'] != "") &&
(isset($_POST['user_email']) && $_POST['user_email'] != "") &&
(isset($_POST['user_password']) && $_POST['user_password'] != "") &&
(isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") &&
(isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") &&
(isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") &&
(isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") &&
(isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") &&
(isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") &&
(isset($_POST['terms']) && $_POST['terms'] != "")
){
However, based on some variables, I may no longer need any of the billing information. So I am wondering if I can do an IF inside the IF, so something like this:
if(
($billingRequired == 1){
(isset($_POST['billing_company']) && $_POST['billing_company'] != "") &&
(isset($_POST['billing_address']) && $_POST['billing_address'] != "") &&
(isset($_POST['billing_city']) && $_POST['billing_city'] != "") &&
(isset($_POST['billing_state']) && $_POST['billing_state'] != "") &&
(isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") &&
(isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") &&
}
(isset($_POST['location_name']) && $_POST['location_name'] != "") &&
(isset($_POST['location_address']) && $_POST['location_address'] != "") &&
(isset($_POST['location_city']) && $_POST['location_city'] != "") &&
(isset($_POST['location_state']) && $_POST['location_state'] != "") &&
(isset($_POST['location_zip']) && $_POST['location_zip'] != "") &&
(isset($_POST['location_phone']) && $_POST['location_phone'] != "") &&
(isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") &&
(isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") &&
(isset($_POST['user_username']) && $_POST['user_username'] != "") &&
(isset($_POST['user_email']) && $_POST['user_email'] != "") &&
(isset($_POST['user_password']) && $_POST['user_password'] != "") &&
(isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") &&
(isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") &&
(isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") &&
(isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") &&
(isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") &&
(isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") &&
(isset($_POST['terms']) && $_POST['terms'] != "")
){
I'm pretty sure there isn't but wanted to check with people smarter than me. I know I can do some nesting in side the {} but wanted to not have to check each variable inside a very deep nest.
Thanks,
Just add () around the section you want to combine and it will resolve to a simple boolean which can be included in your if statement
The logic here reads "if (billing is not required or billing fields are all filled out) and all the non-billing fields are filled out, then..."
if(
(($billingRequired != 1) || (
(isset($_POST['billing_company']) && $_POST['billing_company'] != "") &&
(isset($_POST['billing_address']) && $_POST['billing_address'] != "") &&
(isset($_POST['billing_city']) && $_POST['billing_city'] != "") &&
(isset($_POST['billing_state']) && $_POST['billing_state'] != "") &&
(isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") &&
(isset($_POST['billing_phone']) && $_POST['billing_phone'] != "")
))
&&
(
(isset($_POST['location_name']) && $_POST['location_name'] != "") &&
(isset($_POST['location_address']) && $_POST['location_address'] != "") &&
(isset($_POST['location_city']) && $_POST['location_city'] != "") &&
(isset($_POST['location_state']) && $_POST['location_state'] != "") &&
(isset($_POST['location_zip']) && $_POST['location_zip'] != "") &&
(isset($_POST['location_phone']) && $_POST['location_phone'] != "") &&
(isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") &&
(isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") &&
(isset($_POST['user_username']) && $_POST['user_username'] != "") &&
(isset($_POST['user_email']) && $_POST['user_email'] != "") &&
(isset($_POST['user_password']) && $_POST['user_password'] != "") &&
(isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") &&
(isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") &&
(isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") &&
(isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") &&
(isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") &&
(isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") &&
(isset($_POST['terms']) && $_POST['terms'] != "")
)
{
//statements
}
Although I suspect this could be done better within a loop, maybe it's personal preference, but I'd be happier with something like this
$billingRequiredFields = array('billing_company','billing_address','billing_city','billing_state','billing_zip','billing_phone','location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms');
$billingNotRequiredFields = array('location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms')
$requiredFields = ($billingRequired == 1) ? $billingRequiredFields : $billingNotRequiredFields;
$continue = true;
foreach($requiredFields as $field) {
if (!isset($_POST[$field]) || $_POST[$field] == '') {
$continue = false;
break;
}
}
if ($continue) {
// statements
}
What I would do is create an array at the top of the script that contains a list of all the POSTed variables that are required, then I would just loop through them. Your code would be way smaller. and almost as concise....
//We NEED these fields for the script to work...
$requiredFields = array(
"fname",
"lname",
"phone",
"accredited",
"etc, etc"
);
//IF you require billing,
if($billingRequired){
// Define the billing fields that we expect to be POSTed...
$billingFields = array(
"billing_compnay",
"billing_address",
"etc, etc"
);
// Add the billing fields to the required fields
$requiredFields = array_merge($requiredFields, $billingFileds);
}
// Loop through required fields and check to see if they are all POSTed
foreach($requiredFields as $fieldName){
// IF a required field is not set...
if(empty($_POST[$fieldName])){
// Do stuff, call a function, show an error, etc.
break; // Or redirect, or exit after a JSON response, whatever. Just be sure to end the loop here for efficiency.
}
}
better yet, don't put so many tests in the primary conditional, just make a function to test it, and then test for === true or === false
function validate_input($billingRequired=0){
$b_valid = true;
if( $billingRequired == 1 ){
if (!isset($_POST['billing_company']) || $_POST['billing_company'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_address']) || $_POST['billing_address'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_city']) || $_POST['billing_city'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_state']) || $_POST['billing_state'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_zip']) || $_POST['billing_zip'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['billing_phone']) || $_POST['billing_phone'] == ""){
$b_valid = false;
}
}
if (!isset($_POST['location_name']) || $_POST['location_name'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_address']) || $_POST['location_address'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_city']) || $_POST['location_city'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_state']) || $_POST['location_state'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_zip']) || $_POST['location_zip'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['location_phone']) || $_POST['location_phone'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_firstname']) || $_POST['user_firstname'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_lastname']) || $_POST['user_lastname'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_username']) || $_POST['user_username'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_email']) || $_POST['user_email'] != "") == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_password']) || $_POST['user_password'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['user_mobile']) || $_POST['user_mobile'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_name']) || $_POST['payment_cc_name'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_number']) || $_POST['payment_cc_number'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_expo_month']) || $_POST['payment_cc_expo_month'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_expo_year']) || $_POST['payment_cc_expo_year'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['payment_cc_code']) || $_POST['payment_cc_code'] == ""){
$b_valid = false;
}
elseif (!isset($_POST['terms']) || $_POST['terms'] == "")
$b_valid = false;
}
return $b_valid;
}
now it's easy to modify / read etc. Because empty can be slightly ambiguous I find myself avoiding it as a rule, despite it's stylistic elegance.
To make this even cleaner, I'd probably write it like this:
function validate_input($billingRequired=0){
$b_valid = true;
$a_billing = array('billing_company','billing_address'...);
$a_main = array('billing_address','location_address'...);
if( $billingRequired == 1 ){
$a_main = array_merge($a_billing,$a_main);
}
foreach ($a_main as $test){
if (!isset($_POST[$test]) || trim($_POST[$test]) == "")
$b_valid = false;
break;
}
}
return $b_valid;
}
With the caveat that assumes empty values are "", which wouldn't generally be the case for select lists etc.

php function echo

I have:
$an = "1989";
$luna = "4";
$zi = "23";
function CalzulareZodie($date){
list($an,$luna,$zi)=explode("-",$date);
if(($luna==1 && $day>20)||($month==2 && $zi<20)){
return "Varsator";
}else if(($luna==2 && $zi>18 )||($luna==3 && $zi<21)){
return "Pesti";
}else if(($luna==3 && $zi>20)||($luna==4 && $zi<21)){
return "Berbec";
}else if(($luna==4 && $zi>20)||($luna==5 && $zi<22)){
return "Taur";
}else if(($luna==5 && $zi>21)||($luna==6 && $zi<22)){
return "Gemeni";
}else if(($luna==6 && $zi>21)||($luna==7 && $zi<24)){
return "Rac";
}else if(($luna==7 && $zi>23)||($luna==8 && $zi<24)){
return "Leu";
}else if(($luna==8 && $zi>23)||($luna==9 && $zi<24)){
return "Fecioara";
}else if(($luna==9 && $zi>23)||($luna==10 && $zi<24)){
return "Balanta";
}else if(($luna==10 && $zi>23)||($luna==11 && $zi<23)){
return "Scorpion";
}else if(($luna==11 && $zi>22)||($luna==12 && $zi<23)){
return "Sagetator";
}else if(($luna==12 && $zi>22)||($luna==1 && $zi<21)){
return "Capricorn";
}
}
how can i echo the result of this function?
i've tried with:
$zodia=CalculareZodie();
echo "Zodia este: ".$zodia;
What is wrong?
Your function takes an argument. You are not passing one.
$zodia=CalculareZodie($somedate);
You've also misspelled it.
You forgot to pass argument. Also check the spelling CalzulareZodie
$zodia= CalzulareZodie('1989-4-23');
echo "Zodia este: ".$zodia;
You should pass the date as function's argument.
Also note that the variable declared outside the function are not available within the function. Either you have to declare them inside function or use global variable scope
$an = "1989";
$luna = "4";
$zi = "23";
function CalzulareZodie($date){
global $an, $luna, zi; // If you want
list($an,$luna,$zi)=explode("-",$date);
if(($luna==1 && $day>20)||($month==2 && $zi<20)){
return "Varsator";
}else if(($luna==2 && $zi>18 )||($luna==3 && $zi<21)){
return "Pesti";
}else if(($luna==3 && $zi>20)||($luna==4 && $zi<21)){
return "Berbec";
}else if(($luna==4 && $zi>20)||($luna==5 && $zi<22)){
return "Taur";
}else if(($luna==5 && $zi>21)||($luna==6 && $zi<22)){
return "Gemeni";
}else if(($luna==6 && $zi>21)||($luna==7 && $zi<24)){
return "Rac";
}else if(($luna==7 && $zi>23)||($luna==8 && $zi<24)){
return "Leu";
}else if(($luna==8 && $zi>23)||($luna==9 && $zi<24)){
return "Fecioara";
}else if(($luna==9 && $zi>23)||($luna==10 && $zi<24)){
return "Balanta";
}else if(($luna==10 && $zi>23)||($luna==11 && $zi<23)){
return "Scorpion";
}else if(($luna==11 && $zi>22)||($luna==12 && $zi<23)){
return "Sagetator";
}else if(($luna==12 && $zi>22)||($luna==1 && $zi<21)){
return "Capricorn";
}
}
and
$zodia=CalzulareZodie("Enter your date here");
echo "Zodia este: ".$zodia;
You need to pass the variables to the function, now, judging by the start of the function it takes a string with a date separated by dashes as an arguement so you'll need to do something like:
$an = "1989";
$luna = "4";
$zi = "23";
$date = $an . '-' . $luna . '-' . $zi; //Construct the string from the outside variables
Then you do:
$zodia=CalzulareZodie($date); //Pass constructed string to function.
echo "Zodia este: ".$zodia;
$zodia=CalculareZodie('specify date here');
There are a couple of things wrong.
You have a date argument which you are not passing in.
The variables are not accessible in the function and need to be set as global like this:
function CalzulareZodie($date){
global $an, $luna, $zi;
// REST OF THE FUNCTION
}

Categories