Help!! Inexplicable PHP issue - driving me further into insanity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adrian.price@gmail.com

    Help!! Inexplicable PHP issue - driving me further into insanity

    Okay. I've been staring at this for 48 hours now, with absolutely no
    luck. Every time I try to break it down further, it makes less sense.
    I've now broken this down to such a level that I can't think of any way
    this can even happen. But enough of that. Take this code:

    $x = "( ( !$enrollment_id || !$sch_id || \"".$course['state']."\" !=
    \"approved\" ) && !".(int)
    $auth->check_perm('lm s','submit_cour se',PERM_READ). " )";
    $y = ( ( !$enrollment_id || !$sch_id || $course['state'] != 'approved'
    ) && !$auth->check_perm('lm s','submit_cour se',PERM_READ) );

    debug($x,$y);

    if ( $y )
    debug($x,$y);

    if ( ( !$enrollment_id || !$sch_id || $course['state'] != 'approved' )
    && !$auth->check_perm('lm s','submit_cour se',PERM_READ) )
    { /* real code here */ }


    $auth->check_perm returns a boolean. In this context, $enrollment_id
    and $sch_id are provided and non-zero, and $course['state'] =
    "approved". The function debug() spits out all the provided arguments
    along with some debugging info, and serves as a breakpoint. Now. Out of
    150 users of this system, 4 have experienced an issue where, when that
    if should be false, it comes out true, reliably, on the server and my
    test platform. There's a lot of background information, some of which I
    can provide (though I have to be somewhat vague, as this is proprietary
    code), but I don't think I need to, and I'll explain why:

    If you leave the first breakpoint in, you get this:
    Debugging: array (
    0 => '( ( !36 || !19 || "approved" != "approved" ) && !0 )',
    1 => false,
    )

    If you comment out the first breakpoint, it breaks inside the if
    statement, even though it just said the value in the if expression is
    false:
    Debugging: array (
    0 => '( ( ! || ! || "approved" != "approved" ) && !0 )',
    1 => true,
    )

    So, my question is, how can the values referenced in the assignment of
    $x and $y change based on whether or not those variables, or variables
    assigned using data from those variables, are in an if statement? If
    you can explain that to me, I'd be thrilled, because I have a headache
    the size of siberia right now!

    (The code above is obviously incomplete, but it is copy-and-pasted from
    the source file unaltered, save for commenting out the code at the very
    end. And I don't really see how any other code in the file could affect
    it in this situation, but obviously something is.)

    If you can give even a clue or a direction to look in, I'd hugely
    appreciate it!!!

  • jonathan.beckett

    #2
    Re: Help!! Inexplicable PHP issue - driving me further into insanity

    As a starting point, I would re-write your if statements.

    You're currently writing something similar to this...

    if ( 1 || 2 || 3 != 4)

    you should really be doing...

    if ( (1!=4) || (2!=4) || (3!=4) )


    Hope that makes sense.

    Comment

    • adrian.price@gmail.com

      #3
      Re: Help!! Inexplicable PHP issue - driving me further into insanity

      Well, while a comparison wouldn't work here, I could use empty($var)
      explicitly instead of just if($var), but it really doesn't have any
      impact, as PHP converts if($var) to if(empty($var)) , the only
      difference is, empty() won't choke if the variable hasn't been defined.

      Comment

      • Jim Michaels

        #4
        Re: Help!! Inexplicable PHP issue - driving me further into insanity


        <adrian.price@g mail.com> wrote in message
        news:1140015879 .961893.166540@ g47g2000cwa.goo glegroups.com.. .[color=blue]
        > Okay. I've been staring at this for 48 hours now, with absolutely no
        > luck. Every time I try to break it down further, it makes less sense.
        > I've now broken this down to such a level that I can't think of any way
        > this can even happen. But enough of that. Take this code:
        >[/color]
        $x = "( ( !$enrollment_id || !$sch_id || \"$course[state]\" !=
        \"approved\" ) && !".(int)
        $auth->check_perm('lm s','submit_cour se',PERM_READ). " )";
        $y = ( ( !$enrollment_id || !$sch_id || $course['state'] != 'approved'
        ) && !$auth->check_perm('lm s','submit_cour se',PERM_READ) );

        your $auth->check_perm() is returning an int instead of true or false. I
        hope it is returning either 0 or a nonzero value where 0 is interpreted as
        false.
        I also hope none of your id's or $course[] are NULL.

        C:\www\site tools\php\mathp arse\phpdll>php
        <?php print !0; ?>
        ^Z
        1
        C:\www\site tools\php\mathp arse\phpdll>php
        <?php print !5; ?>
        ^Z

        <?php if (!5) print 1; else print 0; ?>
        ^Z
        0

        The "no result" was actually the result false and must be eventually tested
        with an if to do something useful.
        [color=blue]
        > debug($x,$y);
        >
        > if ( $y )
        > debug($x,$y);
        >
        > if ( ( !$enrollment_id || !$sch_id || $course['state'] != 'approved' )
        > && !$auth->check_perm('lm s','submit_cour se',PERM_READ) )
        > { /* real code here */ }
        >
        >
        > $auth->check_perm returns a boolean. In this context, $enrollment_id
        > and $sch_id are provided and non-zero, and $course['state'] =
        > "approved". The function debug() spits out all the provided arguments
        > along with some debugging info, and serves as a breakpoint. Now. Out of
        > 150 users of this system, 4 have experienced an issue where, when that
        > if should be false, it comes out true, reliably, on the server and my
        > test platform. There's a lot of background information, some of which I
        > can provide (though I have to be somewhat vague, as this is proprietary
        > code), but I don't think I need to, and I'll explain why:
        >
        > If you leave the first breakpoint in, you get this:
        > Debugging: array (
        > 0 => '( ( !36 || !19 || "approved" != "approved" ) && !0 )',
        > 1 => false,
        > )
        >
        > If you comment out the first breakpoint, it breaks inside the if
        > statement, even though it just said the value in the if expression is
        > false:
        > Debugging: array (
        > 0 => '( ( ! || ! || "approved" != "approved" ) && !0 )',
        > 1 => true,
        > )
        >
        > So, my question is, how can the values referenced in the assignment of
        > $x and $y change based on whether or not those variables, or variables
        > assigned using data from those variables, are in an if statement? If
        > you can explain that to me, I'd be thrilled, because I have a headache
        > the size of siberia right now!
        >
        > (The code above is obviously incomplete, but it is copy-and-pasted from
        > the source file unaltered, save for commenting out the code at the very
        > end. And I don't really see how any other code in the file could affect
        > it in this situation, but obviously something is.)
        >
        > If you can give even a clue or a direction to look in, I'd hugely
        > appreciate it!!!
        >[/color]


        Comment

        Working...