Home > php教程 > php手册 > body text

[Transfer] Three ways to mix php and html

WBOY
Release: 2016-08-15 16:49:41
Original
1784 people have browsed it

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:

<html>
<head> 
<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</span>-size:15px;color:<span style="color: #008000;">#</span><span style="color: #008000;">000;font-family:Arial,Helvetica,sans-serif;}</span>
        a{color:<span style="color: #008000;">#</span><span style="color: #008000;">039;text-decoration:none;}</span>
    </style> 
</head>
<body>
        </b> 
        <?<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><span style="color: #008000;"> >这是一个超链接</a>
        </b>
    </body>
</html>
Copy after login


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 style="color: #000000;">; 
    } 
</span>?>
Copy after login
However, it is more common to add escape symbols, which personally makes it uncomfortable to read
echo "<input type=\"submit\" value=\"确定\"/>"
Copy after login

 


The third method is to use the (<<<) tag, which is first seen in the template code of PHP168.
<<<EOT 
EOT;
Copy after login

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> <<<<span style="color: #000000;">EOT
        </span><div <span style="color: #0000ff;">class</span>="slidecont">{<span style="color: #800080;">$label1</span>}</div> 
        <div <span style="color: #0000ff;">class</span>="newcontainter"> 
        <div <span style="color: #0000ff;">class</span>="head">{<span style="color: #800080;">$label2</span>}</div> 
        <div <span style="color: #0000ff;">class</span>="cont" id="Tab1"><span style="color: #000000;"> 
            {</span><span style="color: #800080;">$label3</span><span style="color: #000000;">} 
        </span></div> 
        <div <span style="color: #0000ff;">class</span>="cont" id="Tab2"><span style="color: #000000;"> 
            {</span><span style="color: #800080;">$label4</span><span style="color: #000000;">} 
        </span></div> 
        </div> 
        <a href="<span style="color: #800080;">$rs</span>[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>?>
Copy after login


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> <<<<span style="color: #000000;">EOT<span style="color: #ff0000;">[1]
    </span></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>?>
Copy after login

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 '<<' (T_SL) in D:\MyHTTP\Apache24\htdocs\testPHPmixHTML04.php on line 2
    //[2]( ! ) Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in D:\&hellip;&hellip;\testPHPmixHTML04.php on line 14
    //[3]( ! ) Parse error: syntax error, unexpected '{' in D:\MyHTTP\Apache24\htdocs\testPHPmixHTML04.php on line 8</span>
Copy after login

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.
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template