0

I'm having a strange behavior with a section (more precisely a conditional statement) of a PHP script.

When using a breakpoint on the exact line of the conditional with XDebug (phpStorm), the script executes as expected. Inversely, when executing normally or breaking anywhere else (before or inside brackets), the supposedly true condition is skipped.

if($interval > $maxCovering) //2 DateInterval objects - break here and it works
{
  //Do something - never reached on standard execution
}

I tried to pause the execution right before the line and it doesn't help. Also, I did some research and came up empty regarding this problem with PHP.

What can cause this behavior and what are the remedies?

EDIT Those are the typical logs: 'max' and 'inter' are arguments and 'compar' is the comparison First 2 lines are execution with breakpoints and last 2 without

enter image description here

user3311142
  • 345
  • 1
  • 4
  • 13
  • Print the values of both variables for debugging. Maybe the difference between using and not using the breakpoint comes clear. – RhinoDevel Mar 10 '16 at 18:25
  • 2
    Have you tried logging (into a file) the variable values before doing comparison? The difference during runtime (especially if no xdebug extension is loaded at all) can make the whole difference (xdebug, even if you do not debug but just having extension loaded, makes your code slower) – LazyOne Mar 10 '16 at 18:25
  • @RhinoDevel Thanks for the tip! I posted the logs. – user3311142 Mar 10 '16 at 19:20
  • @LazyOne I tried inactivate XDebug on chrome. The same also happens on firefox. I posted the logs with breakpoints and without XDebug (debug mode+extension). – user3311142 Mar 10 '16 at 19:23
  • If you extract your code into standalone file/files and create new project from that files/those files -- is it working fine there? If it still has the same issue -- can you share such file(s)? – LazyOne Mar 10 '16 at 20:00
  • @LazyOne Thanks for the help. Turned out the problem was caused by DateTime interval object not implementing comparison unless evaluated first [link](http://stackoverflow.com/questions/9547855/are-php-dateinterval-comparable-like-datetime). So when breaking at comparison line, variables get evaluated and the remaining execution is valid. I might delete the question as a whole since it's not directly related to the original question. – user3311142 Mar 13 '16 at 14:57
  • 1
    @user3311142 Better mark it as an answer -- other people may face the same issue and finding yours might be easier than the linked one (different keywords/tags etc) -- you can accept your own answer (few days later, AFAIK). In short: "it's not PhpStorm/xdebug issue but the fact that DateTimeInterval is not comparable. here is how I resolved it for myself ..." – LazyOne Mar 13 '16 at 16:00

1 Answers1

0

The problem here is not caused by PhpStorm or XDebug but instead by the DateInterval class which is not comparable unless evaluated first. This is a known issue : SO question on the topic.
To patch the problem, I use the very inelegant solution of artificially evaluate it before the comparison, so the code becomes:

var_dump($interval);
var_dump($maxCovering);
if($interval > $maxCovering) //2 DateInterval objects
{
  //Do something
}

Answer to question quoted above also provides link to a patch.

Community
  • 1
  • 1
user3311142
  • 345
  • 1
  • 4
  • 13