SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Intelligence of parser when dealing with condition

    PHP Code:
    $foo false;
    $bar true;
    if (
    $foo && $bar) {
      
    doSomething();

    Since the order there is "if $foo AND $bar", does the parser stop processing the if statement when it concludes $foo is false, or will it also evaluate $bar regardless? If $foo is false, then it doesn't matter what $bar is, the whole statement resolves to false, so I wonder if the parser realises this.

    I'd also be interested to know if this is the same for Javascript if anyone knows.

  2. #2
    SitePoint Evangelist Andrewaclt's Avatar
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    535
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, typically the parsers do, it's called short-circuit evaluation. I would assume almost all modern languages do it, including javascript. You can write code to test it easily, so why not try it?

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah, if I make $bar a function that'll spit something out I can test it... didn't occur to me earlier. Cheers Andrew.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •