Unexpected T_ELSE Statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Unexpected T_ELSE Statement

Post by a94060 »

Hi, I have this script here and im not sure why i am getting this error:

My script is :

Code: Select all

<?PHP
session_start();
//simple check to see if the person is logged in
if($_SESSION['in'] != TRUE) {
echo 'You are not logged in,please log in at the admin area.';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Page</title>
<style type="text/css">
<!--
.style1 {font-size: 16px}
.style4 {font-size: 24px}
-->
</style>
</head>

<body>
<h1>
  <?PHP
else {
?>
</h1>
<div align="center" class="style1">
  <p class="style4"> Admin Login</p>
  <form id="form1" name="form1" method="post" action="login.php">
    <p>Username
      <input name="you" type="text" id="you" />
  </p>
    <p>Password
      <input name="me" type="text" id="me" />
</p>
    <p>
      <input type="submit" name="Submit" value="Submit" />
      <input type="reset" name="Submit2" value="Reset" />
    </p>
  </form>
  <p>&nbsp;</p>
  <div align="center" class="style1"></div>
</div>
<?PHP
}
?>
</body>
</html>
can anyone tell me what i have done wrong (voided any of the if else statement rules?)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Ignoring all that HTML, your code is:

Code: Select all

if () {

...

}

...

else {

...

}
Maybe separating your code and HTML might help you.
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Unexpected T_ELSE Statement

Post by RobertGonzalez »

Do this...

Code: Select all

<?php
session_start();
//simple check to see if the person is logged in
if($_SESSION['in'] != TRUE) {
echo 'You are not logged in,please log in at the admin area.';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Page</title>
<style type="text/css">
<!--
.style1 {font-size: 16px}
.style4 {font-size: 24px}
-->
</style>
</head>

<body>
<h1>
<?php
} else {
?>
</h1>
<div align="center" class="style1">
  <p class="style4"> Admin Login</p>
  <form id="form1" name="form1" method="post" action="login.php">
    <p>Username
      <input name="you" type="text" id="you" />
  </p>
    <p>Password
      <input name="me" type="text" id="me" />
</p>
    <p>
      <input type="submit" name="Submit" value="Submit" />
      <input type="reset" name="Submit2" value="Reset" />
    </p>
  </form>
  <p>&nbsp;</p>
  <div align="center" class="style1"></div>
</div>
<?php
}
?>
</body>
</html>
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

so i guess it was probably a problem with my brackets?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

a94060 wrote:so i guess it was probably a problem with my brackets?
Maybe this makes it clearer:

Code: Select all

if () {

} YOU HAVE TEXT HERE  else {

}
Again, maybe separating your code and HTML might help you.
(#10850)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

so,i had code outside of the brackets? I still dont seem to udnerstand (only 14) can u just tell me.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yep ... with the control structures you need to put code between the { and } (or only have one code line).
(#10850)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

sorry... i did not seem to understand that i had garbage that was outside of the brackets. My fault
Post Reply