We help IT Professionals succeed at work.

Use && vs || in PHP

Horalia Rodriguez
on
269 Views
Last Modified: 2008-01-09
Gosh, I always get mixed up with && and ||, never know when is the most approprite case to use it. I'm adding the capability to add recurring to a personalized calendar that I created in PHP.  The problem is, that when I use && to compare, it takes forever to break a look. Here's the code that I have so you guys can see what I'm talking about:
//ADD, DELETE AND EDIT
switch ($mode)
{
  //ADD
  case "add":
  //IF RECURRENCE TYPE IS DAILY AND DAILY IS EVERY DAY
  if($recurrence_type==1 && $daily==1)
  {
    while ($press_date <= $recurrence_stop)
    {
        $query = "INSERT INTO press_schedule ".
                       "(section_id,press_date,g_lstpte,g_prsstr,g_prsstp,".
                     "prd_dte,section_type,sorc,section_amt,edition,spadia,lap,last_updated) ".
                     "VALUES ".
                     "($sections, '$press_date','$g_lstpte','$g_prsstr','$g_prsstp', ".
                     "'$prd_dte',$section_type,'$sorc', $section_amt, $edition, $spadia, $lap, NOW());";
        mysql_query($query);
        $press_date = date('Y-m-d',strtotime("+1 day, $press_date"));
      }
  //IF RECURRENCE TYPE IS DAILY AND DAILY IS EVERY WEEKDAY
  } elseif($recurrence_type==1 && $daily == 2) {
    while($press_date <= $recurrence_stop)
    {
      if(date('w',strtotime($press_date))!=0 && date('w',strtotime($press_date))!=6)
      {
          $query = "INSERT INTO press_schedule ".
                         "(section_id,press_date,g_lstpte,g_prsstr,g_prsstp,".
                         "prd_dte,section_type,sorc,section_amt,edition,spadia,lap,last_updated) ".
                         "VALUES ".
                         "($sections, '$press_date','$g_lstpte','$g_prsstr','$g_prsstp', ".
                         "'$prd_dte',$section_type,'$sorc', $section_amt, $edition, $spadia, $lap, NOW());";
            mysql_query($query);
            $press_date = date('Y-m-d',strtotime("+1 day, $press_date"));
        }
      }
  }
}
The first while works fine, breaks out of the loop quickly, but the second one won't, it drags on forever. If I compare both date values to where they are not either a Sunday or Saturday (this to add the events only to the weekdays) it will insert the events, but it will also add a Saturday. Help! Thanks...
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
If you find && and || confusing, you can also use "and" and "or" instead

    if ( $a == 1 && $b == 2)

is equivalent to

    if ( $a == 1 and $b == 2 )

See http://uk3.php.net/manual/en/language.operators.logical.php for more details
CERTIFIED EXPERT

Commented:
Yeah - I should have phrased that differently. It does actually say in the link I gave that they are not exactly equivalent.
Horalia RodriguezIT Developer

Author

Commented:
steelseth12, your solution worked great! Could you explain the difference between using != and !==?
!= returns true, if both sides are not equal (type insensitive compare)
!== returns true, if both sides are not equal or are not of same type (binary compare)

4 != '4' //true
4 !== '4' //false

in our example we have 4 as a number and as a string.
Oh i messed:

4 != '4' //false, because they are same, if we don't look at the type
4 !== '4' //true, because they are same value, but not same type
Horalia RodriguezIT Developer

Author

Commented:
Well, when placing an extra '=' in the if statement, the while loop finishes sooner. Is this how its suppose to work? I thought I had something to do with strtotime...
CERTIFIED EXPERT
Top Expert 2007

Commented:
when comparing using == or != , 0 can mean also empty or null. So in your case your script was also checking for empty where is was supposed to look for 0,=== or  !== will check that both sides are identical, same value and same type.

Take a look at
http://www.php.net/operators.comparison
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.