[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use && vs || in PHP

Posted on 2007-10-11
9
Medium Priority
?
246 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...
0
Comment
Question by:horalia
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 20062368
try changing date('w',strtotime($press_date))!=0 to date('w',strtotime($press_date))!==0
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 20063484
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
0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 20065196
no they are not equal!

You still gotta learn about: http://uk3.php.net/manual/en/language.operators.php#language.operators.precedence
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 20066015
Yeah - I should have phrased that differently. It does actually say in the link I gave that they are not exactly equivalent.
0
 

Author Comment

by:horalia
ID: 20067504
steelseth12, your solution worked great! Could you explain the difference between using != and !==?
0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 20067566
!= 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.
0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 20067585
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
0
 

Author Comment

by:horalia
ID: 20067600
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...
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20068228
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question