2017-01-13 109 views
-1

我想它,如果用户没有登录,显示请登录,如果用户在显示内容登录使用if语句,回声在if语句回声中?

这一切的伟大工程,但禁止列为0 =不禁止,1 =禁止 但我想它说,如果禁止== 0则说没有,如果禁止== 1,则说是 但是当我这样做,它给了我一个错误

<? 
if (!$UserLoggedIn) { 

    echo " 
    PLEASE LOGIN 
    "; 
} else { 
    echo " 
    <div style=' 
    background: #FFF; 
    padding: 25px; 
    box-shadow: 0 2px 2px #bbb; 
    margin: auto; 
    position:relative; 
    width: 600px; 
    '> 
    <center> 
    Welcome, $GetLoggedUser->username 
     <br> 
    Here's some statistics about your account: 
     <br> 
    Username: $GetLoggedUser->username 
     <br> 
    Email: $GetLoggedUser->email 
     <br> 
    UserID: $GetLoggedUser->id 
     <br> 
    Banned from game: 
    if($GetLoggedUser->banned == '0') { echo ' no '; } else { echo ' yes '; } 
     <br> 
    Banned from website: $GetLoggedUser->Ban (obviously not or this page wouldn't show up) 
    <center/> 
    <div/> 
    "; 
} 
?> 
+2

不要依赖[short open tags](http://php.net/manual/en/language.basic-syntax.phptags.php)在任何地方工作。它们陈旧,不推荐使用,并且不太可能在配置良好的服务器上工作。 – miken32

回答

0

你不必在一个做的一切回声和代码缩进使得它更易于阅读和调试。

<?php 
    if (!$UserLoggedIn) { 
     echo "PLEASE LOGIN"; 
    }else{ 
     echo "<div style='background: #FFF;padding: 25px;box-shadow: 0 2px #bbb;margin: auto;position:relative;width: 600px;'> 
       <center>Welcome, {$GetLoggedUser->username}<br> 
        Here's some statistics about your account:<br> 
        Username: {$GetLoggedUser->username}<br> 
        Email: {$GetLoggedUser->email}<br> 
        UserID: {$GetLoggedUser->id}<br> 
        Banned from game: "; 
     if($GetLoggedUser->banned == "0") { 
      echo " no "; 
     } else { 
      echo " yes "; 
     } 
     echo "<br>Banned from website: {$GetLoggedUser->Ban} (obviously not or this page wouldn't show up) 
     <center/><div/>"; 
    } 
?> 
+0

解析错误:语法错误,意外'if'(T_IF),期待','或';' – noynac

+0

对不起。在seconnd回声后忘了':'。现在修复 – RiggsFolly

0

你不必回声一切......不是我会怎么做,但下面的例子,你不能做的IF语句内回声字符串,但你可以做一个三元操作,即(价值==真) ?“是”:“没有”,但容易,只需打出来的脚本和回声你需要什么太...

<? 
if (!$UserLoggedIn) { 

    echo "PLEASE LOGIN"; 
}else{ 
?> 
<div style='background: #FFF; 
padding: 25px; 
box-shadow: 0 2px 2px #bbb; 
margin: auto; 
position:relative; 
width: 600px; 
'> 
<center> 
Welcome, <? echo $GetLoggedUser->username ?> 
    <br> 
Here's some statistics about your account: 
    <br> 
Username: <? echo $GetLoggedUser->username ?> 
    <br> 
Email: <? echo $GetLoggedUser->email ?> 
    <br> 
UserID: <? echo $GetLoggedUser->id ?> 
<br> 
Banned from game: 
<? if($GetLoggedUser->banned == "0") { echo " no "; } else { echo " yes ";  } ?> 
    <br> 
Banned from website: <? echo $GetLoggedUser->Ban ?> (obviously not or this page wouldn't show up) 
<center/> 
<div/> 
<? } ?> 
+0

不要依赖[short open tags](http://php.net/manual/en/language.basic-syntax.phptags.php)在任何地方工作。它们陈旧,不推荐使用,并且不太可能在配置良好的服务器上工作。 – miken32

-3

,您仍然可以尝试:从 游戏禁止:

if($GetLoggedUser->banned  == 
"0") { echo " no "; } elseif   ($GetLoggedUser->banned  == "1") { echo " 
yes "; } 
+0

什么?他在一个回声串里面,这是纯粹的废话! – GavinF

0

要显示更大的HTML我倾向于通过关闭并重新打开PHP标签来在PHPHTML之间切换。

我使用echo<?= ?>)的简短语法将PHP的值插入到HTML中。

有些IDE无法解释HTML,当它被封装在PHP,所以当你只是使用echo来输出HTML他们不能syntax highlighting帮助。

<?php 
if (!$UserLoggedIn) { 
    echo "PLEASE LOGIN"; 
} else { 
?> 
    <div style=' 
     background: #FFF; 
     padding: 25px; 
     box-shadow: 0 2px 2px #bbb; 
     margin: auto; 
     position:relative; 
     width: 600px; 
     '> 
     <center> 
      Welcome, <?= $GetLoggedUser->username ?> 
      <br> 
      Here's some statistics about your account: 
      <br> 
      Username: <?= $GetLoggedUser->username ?> 
      <br> 
      Email: <?= $GetLoggedUser->email ?> 
      <br> 
      UserID: <?= $GetLoggedUser->id ?> 
      <br> 
      Banned from game: <?= $GetLoggedUser->banned == '0' ? ' no ' : ' yes ' ?> 
      <br> 
      Banned from website: <?= $GetLoggedUser->Ban ?> (obviously not or this page wouldn't show up) 
     </center> 
    </div> 
<?php 
}