Why doesn’t it work correctly?

Why does file.php output before the id variable becomes non-0.


Answer 1, authority 100%

if ($_POST['id'] != 0){ $id = $_POST['id']; } else { $id = 1; }

It looks like $_POST[‘id’] is null, which means it’s in the else section. In the case of $_GET, exactly the opposite is true. I recommend reading about what == is and what === is. And what is their difference. And even better – read some books on PHP. Looking at this code is the first tip that comes to mind.


Answer 2

if ($id == 0) {
} else {          
   include "file.php";
}

What kind of design is this? Isn’t it easier to immediately check for invalidity?

if ($id != 0) {
   include "file.php";
}

?


Answer 3

Wouldn’t it be easier to write something like this:

if ($_POST['id'] > 0)  $id = $_POST['id'];
if ($_GET['id'] > 0)  include "file.php";  ?