search
Homephp教程php手册[Transfer] Three ways to mix php and html

Three ways to mix php and html

When I studied PHP at W3school, the first sentence I saw was "PHP files can contain text, HTML tags and scripts"

Later when I studied other people's codes, I found that these methods are often used in PHP scripts that require HTML code

The first is to add PHP to HTML. In large sections of html code, in each place where php needs to be executed, . This method is more common in ASP programs.
Example:
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta http-equiv="Content-Language" content="zh-CN"> 
<title>Hello World</title>
    <style><span style="color: #000000;">
        body{font-size:15px;color:<span style="color: #008000;">#<span style="color: #008000;">000;font-family:Arial,Helvetica,sans-serif;}
        a{color:<span style="color: #008000;">#<span style="color: #008000;">039;text-decoration:none;}
    </style> 


         
        <span style="color: #000000;">php 
            </span><span style="color: #0000ff;">echo</span> "Hello world!这是正文"<span style="color: #000000;">; 
        </span>?> 
        <br>
        <a href="<span" style="color: #008000;">#<span style="color: #008000;"> >这是一个超链接</span></a>
        
    


The second method uses echo to output HTML. However, there are double quotes in HTML, and the content output by echo is enclosed in single quotes to avoid errors and save the escaping step.
For example, this code:
<span style="color: #000000;">php 
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_POST</span><span style="color: #000000;">) 
    { 
    </span><span style="color: #0000ff;">echo</span> '<span style="color: #000000;"><form action="" method="post"> 
            服务器地址:<input type="text" name="host" value="localhost"><br> 
            数据库账号:<input type="text" name="user" value=""><br> 
            数据库密码:<input type="password" name="pwd" value=""><br> 
            指定数据库:<input type="text" name="db" value="test"><br> 
            <input type="submit" value="确定"> 
           </form>
<hr>说明:<b>……<br></b>
         </span>'<span style="color: #000000;">; 
    } 
</span>?>
However, it is more common to add escape symbols, which personally makes it uncomfortable to read
echo "<input type=\"submit\" value=\"确定\"/>"

 


The third method is to use the (
<<<EOT 
EOT;

The middle document is output directly. A better way to understand it is "a multi-line echo".
The advantage is that it is convenient to output large sections of HTML, no escaping is required, and variables can be quoted.
An example:
<span style="color: #000000;">php

    </span><span style="color: #800080;">$label1</span> = "deepblue_mainslide"<span style="color: #000000;">;
    </span><span style="color: #800080;">$label2</span> = "deepblue_mainh1"<span style="color: #000000;">;
    </span><span style="color: #800080;">$label3</span> = "deepblue_maint1"<span style="color: #000000;">;
    </span><span style="color: #800080;">$label4</span> = "deepblue_maint2"<span style="color: #000000;">;
    </span><span style="color: #800080;">$rs</span> = <span style="color: #0000ff;">array</span>("http://123.abc.com", "abc", "ABC"<span style="color: #000000;">);
 
    </span><span style="color: #0000ff;">print</span> EOT
        <div style="color: #0000ff;">class="slidecont">{<span style="color: #800080;">$label1</span>}</div> 
        <div style="color: #0000ff;">class="newcontainter"> 
        <div style="color: #0000ff;">class="head">{<span style="color: #800080;">$label2</span>}</div> 
        <div style="color: #0000ff;">class="cont" id="Tab1"><span style="color: #000000;"> 
            {</span><span style="color: #800080;">$label3</span><span style="color: #000000;">} 
        </span>
</div> 
        <div style="color: #0000ff;">class="cont" id="Tab2"><span style="color: #000000;"> 
            {</span><span style="color: #800080;">$label4</span><span style="color: #000000;">} 
        </span>
</div> 
        </div> 
        <a href="<span%20style=" color:>$rs[0]" title="<span style="color: #800080;">$rs</span>[1]" target="_blank"><span style="color: #800080;">$rs</span>[2]</a><span style="color: #000000;"> 
EOT;
</span>?>


The above example cleanly outputs a large section of HTML+variable values, which is great.
But there is one more thing to note when using the (
The end of the identifier string is EOT; it must occupy an exclusive line, and no more content is allowed before or after it.
PS. Note that not only can there be no spaces before EOT;, but also there cannot be any spaces after


A particularly common mistake is to add spaces and indents before identifiers. The following example will output nothing

<span style="color: #000000;">php 
    </span><span style="color: #0000ff;">print</span> EOT<span style="color: #ff0000;">[1]
    </span><a href="http://blog.i1728.com/" title="东方天宇的博客">东方天宇的博客</a><span style="color: #000000;"><span style="color: #ff0000;"> 
[2]</span>EOT;<span style="color: #ff0000;">[3]
    </span></span><span style="color: #0000ff;">echo</span> "喂~人家在等你呢!"<span style="color: #000000;">; 
</span>?>

We will find that [if spaces are added to any of the three places], the following three different error prompts will be displayed, and the final echo will not be executed.

    <span style="color: #008000;">//</span><span style="color: #008000;">[1]( ! ) Parse error: syntax error, unexpected '</span>

This is because (the end character of the identifier is not found, and the echo statement is also included in the content that needs to be entered.

At the same time, all content will not be output because the end character of the identifier is not found.
Of course, this is just my understanding, because even if the echo statement is placed in front of print , the script will not have content.
The specific reasons are left to everyone to discuss.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function