skip to Main Content

When writing code in a .php file starting <?php, I can comment on the PHP script by adding // at the front of the line.

But when starting code with <!DOCTYPE the // just doesn’t work. When I comment with this <!–, it only comments out the HTML code and left alone, the php code.

I’m using PHPStorm 2023.

Here’s my code:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.css">
    <title>Document</title>
    <style>
        body {
            margin-top: 30px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="column column-60 column-offset-20">
                <h1>Our first Form</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis, dicta eligendi eveniet modi nostrum porro reiciendis suscipit tempora temporibus veniam?</p>

                <p>
                    <?php if (!empty($_POST['fname'])){ ?>
                    //First name: <?php echo $_POST['fname']; ?> <br>
                    <?php } ?>

                    <?php if (!empty($_POST['lname'])): ?>
                    Last Name: <?php echo $_POST['lname']; ?><br>
                    <?php endif ?>
                </p>

                <div class="myForm">
                    <form method="POST">
                        <label for="fname">First Name</label>
                        <input type="text" id="fname" name="fname">
                        <label for="lname">Last Name</label>
                        <input type="text" id="lname" name="lname">
                        <button type="submit" name="submit" id="submit">Submit</button>
                    </form>
                </div>
            </div>

        </div>
    </div>
</body>
</html>

I’ve tried both php comment and html comment, but nothing worked. Html comment just comment out the html codes not php script.

What I want: I want to use // to comment on any line on a .php file. Either it’s a script or html codes inside a .php file.

Note: I’m very new at PHP in general. Please pardon any terms I’ve used that don’t make any sense.

Thank you!

2

Answers


  1. To comment out the line, use this:

     <!-- First name: <?php /* echo $_POST['fname']; */?> <br>  -->
    
    Login or Signup to reply.
  2. <?php if (!empty($_POST['fname'])){ ?>
    //First name: <?php echo $_POST['fname']; ?> <br>
    <?php } ?>
    

    Forward slashes do not have any special meaning in HTML. HTML comments look like <!-- Your comment here -->. To use PHP comments you need to write it as PHP code:

    <?php if (!empty($_POST['fname'])){ ?>
    <?php //First name: <?php echo $_POST['fname']; ?> <br>?>
    <?php } ?>
    

    or

    <?php if (!empty($_POST['fname'])){ /*?>
    First name: <?php echo $_POST['fname']; ?> <br>
    <?php */} ?>
    

    Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search