PHP or HTML first or does it matter?

13,075

Solution 1

The third one is the correct way (assuming you want the text to echo out in the body).

PHP can jump in and out of HTML as you have shown above.

<html>
<head>
</head>
<body>
<center>
<font size="2"><?php echo "This is text"; ?></font>
</center>
</body>
</html>

Solution 2

Personally I put the PHP as much as possible at the top of the page or even better outside the html page completely by using the html pages as purely views in the MVC pattern.

Solution 3

HTML doesn't go anywhere, but PHP script goes to server executes and response is returned to client side. Now that response is displayed/handled along with HTML code. HTML is only for browser where PHP script is used invoke service or do operations on database. So, first PHP(Server) and then HTML(Client).

Solution 4

Add your php code before the html code.

This allows you to change the out type, set requied variables, add http response headers if you require, etc.

You can have a lot of php embeded tags in between the html.

The html in your question would be invalid, if you echoed output before or after . Make sure your out is valid html.

Don't be bad to the browser just cause they will try to work with whatever you give them.

Solution 5

Onlything you have to maintain valid html structure. so you canot put anything outside the html tag. so third option is the most valid thing. but if you use any of others, it will print anything you want.

Share:
13,075
Funk Forty Niner
Author by

Funk Forty Niner

Updated on June 19, 2022

Comments

  • Funk Forty Niner
    Funk Forty Niner almost 2 years

    I have a possible stupid question, but I'll ask it anyway.

    Does it matter what goes first, PHP or HTML code?

    For example: Does PHP go before HTML, after HTML or does it matter at all?

    <?php
    
    echo "This is text";
    
    ?>
    
    
    <html>
    <head>
    </head>
    <body>
    <center>
    <font size="2">This is text</font>
    </center>
    </body>
    </html>
    

    Or:

    <html>
    <head>
    </head>
    <body>
    <center>
    <font size="2">This is text</font>
    </center>
    </body>
    </html>
    
    <?php
    
    echo "This is text";
    
    ?>
    

    Or:

    <html>
    <head>
    </head>
    <body>
    
    <?php
    
    echo "This is text";
    
    ?>
    
    </body>
    </html>