PhpStorm中Xdebug的条件断点

时间:2018-03-08 13:54:08

标签: php phpstorm

让我们假设我们有这两种方法:

function superFunction($superhero,array $clothes){
    if($superhero===CHUCK_NORRIS){
      wear($clothes);
    } else if($superhero===SUPERMAN) {
      wear($clothes[1]);
    }
}

function wear(array $clothes)
{
   for($piece in $clothes){
       echo "Wearing piece";
   }
}

所以我想要实现的是将PhpStorm中的断点放入函数wear但我只想在$superhero变量具有值CHUCK_NORRIS时才会被触发我该怎么做。想象一下,函数superFunction被调用了数十万次,并且一直按下 F9 会产生反作用。

2 个答案:

答案 0 :(得分:2)

正如我已经评论过的,至少有两种方法可以实现这一点:

  1. 将断点放在函数调用上(在if语句中)并进入函数

    function superFunction($superhero, array $clothes)
    {
        if ($superhero === CHUCK_NORRIS){
            wear($clothes); // <---- put the break point on this line
        } elseif ($superhero === SUPERMAN) {
            wear($clothes[1]);
        }
    }
    
  2. $superhero值作为参数传递给wear函数,并在断点处添加条件,以便仅在$superhero的值为{1}}时停止执行CHUCK_NORRIS

  3. 进入函数

        function superFunction($superhero,array $clothes)
        {
            if ($superhero === CHUCK_NORRIS) {
                wear($clothes, $superhero); // <---- passing the $superhero variable
            } elseif ($superhero === SUPERMAN) {
                wear($clothes[1]);
            }
        }
    
        function wear(array $clothes, $superhero = null)
        {
            for ($piece in $clothes) { // <---- conditional break point here: $superhero === CHUCK_NORRIS
                echo "Wearing piece";
            }
        }
    

答案 1 :(得分:1)

像往常一样将断点放在PhpStorm中,然后在编辑器的装订线上右键单击标记断点的红色圆盘。在打开的弹出窗口中,输入您希望断点停止执行脚本的条件。可以在此处输入在放置断点的代码中有效的任何条件。

例如,输入$superhero===CHUCK_NORRIS

按&#34;完成&#34;按钮,你很高兴。像往常一样调试脚本。每次遇到断点时,调试器都会评估条件,但只有在条件评估为true时才会停止脚本。