簡體   English   中英

使用 if 語句,在 if 語句 echo 內回顯?

[英]Using an if statement, echo inside of an if statement echo?

我想要它,如果用戶未登錄,則顯示 PLEASE LOGIN,如果用戶已登錄,則顯示內容

所有這些都很好用,但是禁止列是 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/>
    ";
}
?>

您不必在一個回顯中完成所有操作,代碼縮進使其更易於閱讀和調試。

<?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/>";
    }
?>

您不必回顯所有內容...不是我將如何執行此操作,而是下面的示例您不能在回顯字符串中執行 If 語句,但您可以執行三元運算,即 (value==true)?"Yes":"No “但更容易打破腳本並回顯你需要的東西......

<?
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/>
<? } ?>

為了顯示更多的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
}

您仍然可以嘗試: 禁止游戲:

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

使用

[英]using <?php brackets inside an echo statement

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM