2017-01-13 155 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 
}