在PHP中执行if和else两个条件

时间:2016-04-22 17:43:06

标签: php

为什么如果 else 两个条件都在if($flw){ ... }内执行

执行程序的内部if条件后,else条件也在执行。

这是我正在使用的课程

class follow extends \Thread{

public $follow;
public $query;

public function connect(){
   $my = new \mysqli(SQLHOST, SQLUSER, SQLPASS, SQLDB);
   return $my;
}

public function run(){}

/*****************************************
*sql queries  related to follow system   *
*@param $x follower $y following         *
******************************************/

public function followSql($x, $y){

    $this->query = array(


        'follow' => "INSERT INTO follow(userid, following) VALUES('$x','$y')",

        'unfollow' => "DELETE FROM follow WHERE userid ='$x' AND following='$y'",

        'following' => "SELECT followid FROM follow WHERE userid='".$x."' AND following='".$y."'"
    );

    return $this->query;
}

/*****************************
*  to follow user            *
*  @param $query sql query   *
*****************************/
public function followDoUndo($q){

    try{

        if($this->connect()){

         mysqli_query($this->connect(), $q);
        }
    }catch(Exception $ex){
        var_dump($ex);
    }
}

/***********************************
*  to show user folllowing or not  *
*  @param $query sql query         *
************************************/
public function following($q){

    try{

        if($this->connect()){

            $result = mysqli_query($this->connect(), $q);

            if (is_object($result)) {
                    if(mysqli_num_rows($result)>0)

                        $this->follow = TRUE;
            }
         return $this->follow;

        }


}catch(Exception $ex){
            var_dump($ex);
        }
    }
}



if($_SERVER["REQUEST_METHOD"] == "POST" ){

    $flw = $_POST["flw"];
}



if($flw){
    if($follow->following($follow->followSql($uid,$ud)['following'])){
        $follow->followDoUndo($follow->followSql($uid,$ud)['unfollow']);
        $showText = "+ follow";
    }
    else{
        $follow->followDoUndo($follow->followSql($uid, $ud)['follow']);
        $showText = "following";
    }   
}

1 个答案:

答案 0 :(得分:1)

if (something) { //The parent IF

  if (something else) { //The child IF
    //do something
  }

  else { //Runs when the child IF is not true.
    //do another
  }

}

完全可以这样说:内部的其他内容不是父母的其他内容。它是兄弟姐妹的