hassan raza hassan raza - 7 months ago 9
PHP Question

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";
}
}

Answer
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