Just curious to know why the code below gives "unexpected T_ELSE" syntax error:
<?php if (isset($_SESSION["user_id"])) { ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php } ?>
<?php else { ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php } ?>
While I keep the } else { on same line, it works fine. I mean the code below just works fine:
<?php if (isset($_SESSION["user_id"])) { ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php } else { ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php } ?>
thinking a bit about this, I've come to the realization that this has to be the intended behavior.
consider the following (syntactical wrong ) example:
<?php if ($condition == true) { ?>
<div id="first">Yey</div>
<?php } ?>
<span id="second?">where am I?</span>
<?php else { ?>
<div id="first">Ney</div>
<?php } ?>
the span element would be in an undefined state
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With