如何正确使用HTML中的PHP if语句? [关闭]

I'm trying to finish up a web page navigation bar by setting different links to appear when a user session is logged but nothing is working.

I've been looking at the same lines of code for hours and can't figure out why the statement is printing both my 'if' and 'else' statements on the nav bar. The code is below. I've tried multiple ways so I'll include all of the ways i have tried to make it easier. Below that code is the code for the entire page. Any help is appreciated!

---------------------------------------------

    <?php 
                    session_start();
                    if (1 = 0){ 
                            <ul id=\"left\">"
                            <li><a href="#"><!--<img id="logo" src="Logo.svg"/>-->Study Studio</a></li>
                            <li><a href="#">Subject</a></li>
                            <li><a href="#">About Us</a></li>
                            <li><a href="#"></a></li>
                        </ul>
                        <ul id="right">
                            <li><input type="search" id="navSearch" placeholder="Search"></li>
                            <li><a href="StudyStudioSignUp.html">Sign Up</a></li>
                            <li><a href="StudyStudioHomePage.html">Login</a></li>
                        </ul>

                    else { 
                        <ul id="left">
                            <li><a href="#"><!--<img id="logo" src="Logo.svg"/>-->Study Studio</a></li>
                    </ul>
                    }?>

--------------------------------------------------------------

 <nav id="headNav"><!-- start of navigation bar -->



            <?php   if (true) : ?>
                    <ul id="left">
                        <li><a href="#">Study Studio</a></li>
                        <li><a href="#">Subject</a></li>
                        <li><a href="#">About Us</a></li>
                        <li><a href="#"></a></li>
                    </ul>
                    <ul id="right">
                        <li><input type="search" id="navSearch" placeholder="Search"></li>
                        <li><a href="StudyStudioSignUp.html">Sign Up</a></li>
                        <li><a href="StudyStudioHomePage.html">Login</a></li>
                    </ul>

            <?php   else: ?>
                    <ul id="left">
                        <li><a href="#">Study Studio</a></li>
                    </ul>
            <?php endif; ?>


        </nav><!-- end nav -->
------------------------------------------------------------------------------


<!doctype html>

<html>
    <?php session_start(); ?>
    <head>
        <meta charset="utf-8" />
        <title>Study Studio - Sign Up</title>
        <!-- Reset page styles  -->
        <link href="reset.css" rel="stylesheet">
        <link href="StudyStudioStyles.css" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Paytone+One" rel="stylesheet">
        <link href="fonts.css" rel="stylesheet">
    </head>

    <header>
        <div id="navImg"> </div>
        <nav id="headNav"><!-- start of navigation bar -->



            <?php   if (true) : ?>
                    <ul id="left">
                        <li><a href="#">Study Studio</a></li>
                        <li><a href="#">Subject</a></li>
                        <li><a href="#">About Us</a></li>
                        <li><a href="#"></a></li>
                    </ul>
                    <ul id="right">
                        <li><input type="search" id="navSearch" placeholder="Search"></li>
                        <li><a href="StudyStudioSignUp.html">Sign Up</a></li>
                        <li><a href="StudyStudioHomePage.html">Login</a></li>
                    </ul>

            <?php   else: ?>
                    <ul id="left">
                        <li><a href="#">Study Studio</a></li>
                    </ul>
            <?php endif; ?>


        </nav><!-- end nav -->
    </header>

    <section id="SignUp">

        <h1>Sign Up</h1>
        <form>
            <input type="text" name="firstName" class="signUp" id="firstName" placeholder="First Name">
            <input type="text" name="lastName" class="signUp" id="lastName" placeholder="Last Name"><br>
            <input type="text" name="email" class="signUp" id="email" placeholder="Email"><br>
            <input type="text" name="school" class="signUp" id="school" placeholder="School"><br>
            <input type="text" name="userName2" class="signUp" id="userName2" placeholder="User Name"><br>
            <input type="password" name="password2" class="signUp" id="SignUp" placeholder="Password">
            <input type="password" name="check" class="signUp" id="check" placeholder="Re-type Password"><br>
            <!--
            <input type="checkbox" name="instructor" class="signUp" id="instructor" value="instructor" onclick="instructor">
            <label for="instructor">Instructor</label><br>
            -->
            <input type="submit" name="submit" id="signUpBtn" value="Sign Up">
        </form>

    </section>

    <section id="section2">

    </section>

    <script>
        document.getElementById('instructor').onclick = function instructor()
        {
            document.getElementById("section2").innerHTML=("added stuff here");
        }
    </script>





    <footer>
                <p id="footer">
                    <!-- information here -->
                </p>

                <!-- three column layout for footer information, if needed -->
                <nav id="end">
                    <ul>
                        <div id="col1">
                            <li><a href="#"></a></li>
                            <li><a href="#"></a></li>
                        </div>
                        <div id="col2">
                            <li><a href="#"></a></li>
                            <li><a href="#"></a></li>
                        </div>
                        <div id="col3">
                            <li><a href="#"></a></li>
                            <li><a href="#"></a></li>
                        </div>
                    </ul>
                </nav>
        </footer>

I'd use simply single quotes with echo

<?php 

session_start();

if (1 = 0) {
   echo '
    <ul id="left">"
        <li><a href="#">Study Studio</a></li>
        <li><a href="#">Subject</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#"></a></li>
    </ul>
    <ul id="right">
        <li><input type="search" id="navSearch" placeholder="Search"></li>
        <li><a href="StudyStudioSignUp.html">Sign Up</a></li>
        <li><a href="StudyStudioHomePage.html">Login</a></li>
    </ul>
    ';

else { 
   echo  '
    <ul id="left">
        <li><a href="#">Study Studio</a></li>
    </ul>
    ';
}

?>

Or to separate your program 'logic' and 'view' - I'd use a variable:

<?php

$html = "";

if ( true ) {
  $html .= '
     <p>Is TRUE</p>
  ';  
} else {
  $html .= '
     <p>Is FALSE</p>
  '; 
}

# other PHP logic here....

?>

Than finally dooown below in HTML simply

<?= $html ?>

Either write it all in php and echo the html using ' for html element attributes:

<?php
if(){
 echo "<p>This is a paragraph</p>";
}
?>

or encase it within html

<html>
<p> <em> 
<?php  
    if(x == y){
      echo "cat"; 
    }else{
      echo "dog";
    }
?> 
</em></p>
</html>

Your code is a mixup of html and php and you have not closed the php tags properly. While writing the html with php , you must have to use this tag

<?php

at the starting of php code and the closing tag ?> at the end of php code.

Let's take an example of your code

<?php 
   session_start();
   if (1 = 0){ 

You have started the html here and didn't close the php tag before html that is ?>

<?php
$this->renderHtmlHead();
?>
<body>
<?php $this->renderHtmlBeforeContent() ?>

<div class="container text-center">
    <?= $core->getPhrase('label_utils_intro') ?><br><br><br>

    <div id="pasteContainer">
        <div class="form-group">
            <h4><?= $core->getPhrase('label_utils_paste') ?></h4><br>
            <textarea class="form-control" id="pastedText" placeholder="<?= $core->getPhrase('label_paste') ?>"></textarea>
        </div><br>
        <div class="form-inline">
            <h5><?= $core->getPhrase('label_utils_quantity') ?><input id="charAmount" type="text" class="form-control numerical-input" placeholder="<?= $core->getPhrase('label_amount') ?>"></h5>
        </div><br><br>
        <div class="form-group">
            <button class="btn btn-success" onclick="return YS.splitText()"><?= $core->getPhrase('label_utils_click_me') ?></button>
        </div>
    </div>
    <div id="resultDiv" style="display: none;">

    </div>
</div>

<?php
$this->addScript('/assets/js/split_text.js');
$this->renderHtmlAfterContent();
$this->renderHtmlScripts();
?>
</body>

Some code to help showcase it, just a simple piece of code. It's better to use the opening and closing tags for PHP, write all HTML code outside of PHP, and when needed use the to insert values. First of all in my opinion, if done correctly, it helps readability and formatting of your code. The above code snippet is very easy to read and understand. Often when you generate HTML code the values are based on your own PHP scoped variables; thus you often end up inserting them in many places.

When using echo (or any other printing method) this can get extremely messy and hard to maintain, missing a comma or a quotation mark can cause a lot of frustration and consequently waste your time.

To start, you have several syntax errors in your php file with the if/else statement.. it doesn't look like you are properly closing the If statement before starting the else statement.