简体   繁体   中英

executing if and else both condition in PHP

Why if and else both conditions are executing inside if($flw){ ... }

After executing the inner if condition of the program, the else condition is also executing.

this is the class which I am using

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";
    }   
}
if (something) { //The parent IF

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

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

}

Totally, you can simply say this: the inner else is not the else for the parent. It's for the sibling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM