samedi 2 juin 2018

XAMPP php executing both if and else parts

It is strange. My php code is executing both if and else parts. Following code starts where $_SESSION['abc'] is not set yet.

//echo $_SESSION['abc'];
if(!isset($_SESSION['abc'])){
  echo "not showing this echo";
  $_SESSION['abc'] = "new";
} else {
  echo " why it jumps here, in XAMPP?";
}

My actual code was more complex so I did it simpler as above. The above code is not echoing "not showing this echo"; even when $_SESSION['abc'] is not set. If I comment out the statement $_SESSION['abc'] = "new"; it echoes "not showing this echo".

What I found out by now:

  1. It is happening only in XAMPP, at production server same is working fine.

  2. It happens when I make the else condition true in the if block. And that condition is setting a session variable.

  3. I myself thought the code is executing twice or something but it is not.
  4. And more importantly "if" condition is true, if you uncomment first line echo $_SESSION['abc'];, it will show undefined index error.
  5. The weirdest feeling for me is, when it reaches $_SESSION['abc'] = "new"; in "if" block, it jumps to "else", ignoring the echo line before it.

Weird!! Am I missing something or it is a XAMPP bug. Yes, the code is working as expected, on production server.

EDIT: I would like to add that only for this behavior I also reinstalled XAMPP (for the latest version).

Aucun commentaire:

Enregistrer un commentaire