chplab
chplab

Reputation: 49

Multiple OR ( || ) And One && In if statement

Will It Work ? I want To send email through chron job. Current Code with only OR (||) is working perfectly. Now I want to add one AND (&&) in this statement. Here $fullDays have OR statemet + I Need To check For pay_confirm column should contain "0".

Will It Work ?

Code is -

$date1= date("Y-m-d");
$date2=date("Y-m-d",strtotime($dateexpire['expiry_date']));
$dateDiff = strtotime($date2) - strtotime($date1);
$fullDays = floor($dateDiff/(60*60*24));

if (($fullDays =="7" || $fullDays =="6" || $fullDays =="5" || $fullDays =="4" || $fullDays =="3" || $fullDays =="2" || $fullDays =="1" || $fullDays =="0") && ($dateexpire["payment_confim"]=="0")){
    ob_start(); 

Upvotes: 0

Views: 137

Answers (4)

thpl
thpl

Reputation: 5910

Best would have been if you tried it out by yourself. Yes it will work. Your if statement is a bit messy though and it's not very efficient to check for every single value like you do. It's not very maintainable and prone to errors. If you feel that an if statement gets messy there's most probably a cleaner and easier way to do it.

$days = range(0,7);

if ( in_array($fullDays, $days) && $dateexpire["payment_confim"] == "0" ){
    ob_start(); 

That'll make your live a lot easier. But yours will work too of course. If you are uncertain read up again how booleans evaluate. That's absolutely essential for survival.

Upvotes: 4

kimbarcelona
kimbarcelona

Reputation: 1136

Your code will definitely work. Seems like you're troubled for testing your code. As a developer, you need to handle the situation easily by using dummy data for testing.You can just declare $fullDays and $dateexpire["payment_confim"] and assign values like:

/*
$date1= date("Y-m-d");
$date2=date("Y-m-d",strtotime($dateexpire['expiry_date']));
$dateDiff = strtotime($date2) - strtotime($date1);
$fullDays = floor($dateDiff/(60*60*24));

*/
 //for testing, assign test value in the variable $fullDays and array $dateexpire["payment_confim"]
    $fullDays = 5;
    $dateexpire["payment_confim"] = 0;
    if (($fullDays =="7" || $fullDays =="6" || $fullDays =="5" || $fullDays =="4" || $fullDays =="3" || $fullDays =="2" || $fullDays =="1" || $fullDays =="0") && ($dateexpire["payment_confim"]=="0")){
        //ob_start(); 
        echo "Conditions met";
     }
     else
     {
      echo "Conditions not met";
     }

Upvotes: 0

Azrael
Azrael

Reputation: 1094

Yes this will work just fine, but it works pretty complicated:

if($banana == 1&&$pear == 2||$mango == 7&& $strawberry == 3)
{
// if code goes here
}

Upvotes: 0

Nerixel
Nerixel

Reputation: 427

This will work.

However, normally there's no point in asking a question on here unless you actually have a problem.

If you were concerned about sending a bad email out to subscribers, you should be testing new code on a separate server that can't email any clients by mistake. That way, even if there is a problem with the code there's no way no one will ever know and you can just fix it without worry.

Upvotes: 0

Related Questions