为什么符合if(){}内的条件,而if外面的还执行,反而写在else内,就不执行了

    public function delete($id)

    {

        // 判断是否有子权限,有则不能删除

        $total = \app\common\model\Auth::where('pid', $id)->count();

        if($total > 0){

            $this->fail('有自己权限,不能删除');

        }

        // 删除数据

        \app\common\model\Auth::destroy($id);

        // 返回数据

        $this->ok();

    }

这样写虽然判断了错误,但是还是会删除我的数据

反而这么写,就不会删除了,,,但是我看别人的if条件之后不跟else一样可以不执行

    public function delete($id)

    {

        // 判断是否有子权限,有则不能删除

        $total = \app\common\model\Auth::where('pid', $id)->count();

        if($total > 0){

            $this->fail('有自己权限,不能删除');

        }else{

            \app\common\model\Auth::destroy($id);

            $this->ok();

 

        }

你的这几个函数,连一个return都没有,就更别说exir停止执行了

public function delete($id)
    {
        // 判断是否有子权限,有则不能删除
        $total = \app\common\model\Auth::where('pid', $id)->count();
        if($total > 0){
           //echo  2 ; //你可以家这个测试是不是执行这里面
            $this->fail('有自己权限,不能删除');
        }
        // 删除数据
        \app\common\model\Auth::destroy($id);
        // 返回数据
        $this->ok();
    }

这个如果 $this->fail('有自己权限,不能删除');这个里没有停止操作,那么不管你if($total > 0){这个if会不会往下指向, // 删除数据
\app\common\model\Auth::destroy($id); 都会执行

public function delete($id)
    {
        // 判断是否有子权限,有则不能删除
        $total = \app\common\model\Auth::where('pid', $id)->count();
        if($total > 0){
          //echo  2 ; //你可以家这个测试是不是执行这里面
            $this->fail('有自己权限,不能删除');
        }else{
         //echo  1; //你可以家这个测试是不是执行这里面
            \app\common\model\Auth::destroy($id);
            $this->ok();
        }
}

至于这个,if条件成立,else当然不会执行了,如果

img