简体   繁体   English

使用 if 语句,在 if 语句 echo 内回显?

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

I want it to, if user is not logged in, display PLEASE LOGIN, and if user is logged in display content我想要它,如果用户未登录,则显示 PLEASE LOGIN,如果用户已登录,则显示内容

all of it works great, but the banned column is 0 = not banned and 1 = banned but i want it to say if banned == 0 then say no and if banned == 1 then say yes but when i do that it gives me an error所有这些都很好用,但是禁止列是 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/>
    ";
}
?>

You do not have to do everything in one echo, and code indenting make it easier to read and debug.您不必在一个回显中完成所有操作,代码缩进使其更易于阅读和调试。

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

You dont have to Echo everything... Not how I would do this but example below you cannot do an If statement INSIDE an echo string, but you could do a ternary operation ie (value==true)?"Yes":"No" but easier to just break out of script and echo what you need too...您不必回显所有内容...不是我将如何执行此操作,而是下面的示例您不能在回显字符串中执行 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/>
<? } ?>

To display bigger amounts of HTML I prefer to switch between PHP and HTML by closing and reopening the PHP tags.为了显示更多的HTML ,我更喜欢通过关闭和重新打开PHP标记来在PHPHTML之间切换。

I am using the short syntax of echo ( <?= ?> ) to insert values from PHP into HTML .我正在使用echo ( <?= ?> ) 的简短语法将PHP中的值插入HTML

Some IDEs can not interpret HTML when it is encapsulated in PHP , so they can not help with syntax highlighting when you are just using echo to output your 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
}

You can still try: Banned from game:您仍然可以尝试: 禁止游戏:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM