Reputation: 69
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 } ?>
Upvotes: 5
Views: 143
Reputation: 2603
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
Upvotes: 1
Reputation: 30488
<?php } ?>
^^
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
When the PHP parser comes to this line it executes the if block only. After that PHP parser tried to parse the next block of code(else part) but here it start with else {
and because of that else
is separated from if
and produces error.
Upvotes: 1
Reputation: 21979
There's nothing weird per se, it's because you're in a separate code block, that's the simplest way to put it. Nothing is open at the time of you "Leaving PHP", so when you go back into it there is no context.
Consider your code like this (of course consider it as pseudo-code just to emphasise the point):
if (isset($_SESSION["user_id"])) {
// ....
}; else {
// ....
}
Breaking in/out of PHP can be tricky at times, and managing it like you want to in your first example doesn't really make very much sense.
You might want to consider using this, which would put your transition to the else
block on a single line anyway:
<?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 endif ?>
At the end of the day whilst PHP is pretty flexible I wouldn't expect it to allow you to do what you're wanting. That would allow for an else
block to be added miles away which may not be the intention at all.
Upvotes: 1
Reputation: 29932
It seems to me, that you can't start a new code block with an else
statement without a preceding if
.
You could…
A) write your code in one block, e.g.
<?php }
else { ?>
B) or use the alternative syntax, if you are working with multiple code-blocks:
<?php if (isset($_SESSION["user_id"])): ?>
/* … */
<?php else: ?>
/* … */
<?php endif; ?>
Upvotes: 1