否则如果 - 我做错了什么?

I have the following code which I use in conjunction with a members script which displays a members username the page or asks guests to login or register.

PHP code:

if ($_SESSION['username'])
{
echo "".$_SESSION['username'].", you are logged in.<br><small><a href=\"logout.php\">Click here to logout</a></small>";
}
else
echo "Welcome Guest!<br><small><a href=\"login.php\">Login</a> or <a href=\"register.php\">Register</a></small>";

It works perfectly well, though now I want to modify it so if a user with admin privileges logs in it identifies the username and offers a link to the admin page.

So here's my modified code:

<? php
$validateadmin = $_SESSION['username'];
if ($validateadmin == "admin1" or $validateadmin == "admin2")
  {
echo "Hello $validateadmin, you have <a href=\"admin.php\">admin</a> privileges.<br><small><a href=\"logout.php\">Click here to logout</a></small>";
  }
else if ($_SESSION['username'])
  {
echo "".$_SESSION['username'].", you are logged in.<br><small><a href=\"logout.php\">Click here to logout</a></small>";
 }
else
  {
echo "Welcome Guest!<br><small><a href=\"login.php\">Login</a> or <a href=\"register.php\">Register</a></small>";
 }
 ?>

Any idea's what I'm doing wrong? It either leaves me with a blank page or errors. I know it's probably a newbie error but for the life of me I don't know what's wrong.

try the following

<?php #removed space
session_start(); #you will need this on all pages otherwise remove it if already called
$validateadmin = $_SESSION['username'];

if($validateadmin == "admin1" || $validateadmin == "admin2"){
    echo "Hello $validateadmin, you have <a href=\"admin.php\">admin</a> privileges.<br><small><a href=\"logout.php\">Click here to logout</a></small>";
}elseif(isset($_SESSION['username'])){ #you should use isset to make sure some variable is set
    echo $_SESSION['username'].", you are logged in.<br><small><a href=\"logout.php\">Click here to logout</a></small>";
}else{
    echo "Welcome Guest!<br><small><a href=\"login.php\">Login</a> or <a href=\"register.php\">Register</a></small>";
}
?>

Generally you should use elseif in php not "else if" because the php parser will interpret else if as else { if { .... }} and you can have some weird errors.

Also, it is a great practice to ALWAYS use braces with control statements to avoid dangling clauses.

Also to avoid notices about array indexes don't do checks like if($array[$index]) if the index may not exist. Use any of array_key_exists, isset, empty, etc (they all are slightly different) to check if an array contains a key you are looking for.