How to embed PHP code in an HTML page

Creating Purchase order form used books

Through this form page, we can know that the customer ordered goods, money orders and other ancillary information. Look at the HTML code below:

<HTML> 
<head>
<title> Wood Xin teacher PHP Basics Tutorial </ title>
</ head>
<body>
<form Action = "processorder.php" Method, = "POST">
<the Table style = "border: 0px; ">
<TR style =" background: #cccccc ">
<TD style =" width: by 150px; text-align = left: Center; "> name Books </ TD>
<TD style =" width: 50px; text-align = left : center; "> number </ td>
</ TR>
<TR>
<td> PHP Getting Started Guide </ td>
<td> <the INPUT of the type =" text "name =" book_name_01 "size =" 3 "maxlength =" 3 "/> </ td>
</ TR>
<TR>
<td> PHP and MySQL development </ td>
<td><input type="text" name="book_name_02" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Laravel入门</td>
<td><input type="text" name="book_name_03" size="3" maxlength="3"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="提交订单"/>
</td>
</tr>
</table>
</form>
</body>
</html>

What access the HTML pages directly through the browser to see the effect:

PHP Basics Lectures: Lesson 2, how to embed PHP code in an HTML page

Page display

Ha, simple page, our Web development has opened a mysterious journey. Keep up!

You may notice one detail: in the form form part of the html code, action we attribute points to a php script:

<form action="processorder.php" method="POST”>

Specific learning PHP script we will soon see. Here are just a little way, this action attribute value is the user clicks the "Submit Order" URL button will be requested.

User-entered data in a form, will POST the way, sent to the URL points to a PHP file for processing.

Form processing

How to deal with this form that it? How to make PHP code work?

To deal with this form, we need to create a php file, its name and the value needed in the form of consistent action attribute.

So, we create a file called processorder.php name it.

So code can be written first, to see if it works Kazakhstan:

<HTML> 
<head>
<title> Order Processing results </ title>
</ head>
<body>
<h1> Wood Xin teacher online library </ h1>
<h2> Order Processing result notification </ h2>
<? php echo '<p> Order processed </ p>'; // here is the PHP code>?
</ body>
</ HTML>

Keep the file and refresh the page. This time we click on "Submit Order" button, the effect is as follows:

PHP Basics Lectures: Lesson 2, how to embed PHP code in an HTML page

php execution results

As you can see, the result is a red line through part of PHP code output. Thus, we achieve the implementation of the PHP code via Web-demand, so a look at PHP is very simple now.

In the way we look at the source code for this page now, look at how PHP code in the HTML page to complete the task bar:

PHP Basics Lectures: Lesson 2, how to embed PHP code in an HTML page

Source

By page source code, we found just write PHP code was gone, replaced by a

<P> Order processed </ p>

This is how it happened?

This is because the PHP interpreter when the script runs, the script's output replaces the script code itself, in this way, you can generate that can run on any browser, the HTML page. In other words, the browser does not need to learn PHP.

By this code, we can learn some of the basics of PHP:

  1. PHP tags
  2. PHP statements
  3. How Space
  4. add notes

PHP tags

第一种情况:在HTML中混写PHP和HTML代码,需要为php添加标记。PHP代码会以“<?php”作为开始,以“?>”作为结束。这些符号就叫做PHP标记,它们主要用来告诉服务器PHP代码的开始和截止,在这两个起止符号之间的任何代码,服务器都会以PHP语法来解析。

另一种情况:之后,我们写纯PHP的时候,每个文件也需要添加PHP标记。不过呢,结束标记可以省略,这也是很大一部分PHPer默认遵守的规则。

PHP语句

在PHP的开始和截止标记之间,就是PHP语句了,通过这些内容可以告诉PHP解释器应该进行如何的操作,在我们这个例子里,通过:

echo '<p>订单已处理完成</p>’;

使用echo语句完成了一个非常简单的操作,仅是将echo后边的字符串原样打印到浏览器中。这里需要特别注意的一点就是每个PHP语句后边都需要添加英文的分号作为语句的结束符,否则会出现错误,但是在这个html页面中,因为只有一句代码,忽略掉分号也是不会报错的。

但是还是强烈建议大家养成习惯:每句PHP代码结束都要以分号结尾哟!

空格

一般情况下,为了让代码更加清晰和整洁,在编码的过程中会添加一些空格,这些空格包括:回车换行、空格、制表符等都被认为是空格。

当然了,浏览器并不会在意你是否输入了空格,同样的PHP服务器端解析器也会忽略这些,这些空格仅是给编写代码的人看的。

但是,木辛老师还是再次强烈建议,在代码的适当位置添加空格或者空行,这样做可以很有效的提升代码的可阅读性,方便后期的维护工作。

注释

最后在讲讲注释,理论上在编程中出现频率非常高的一个知识点。

为什么说理论上呢,因为这么重要的一个要点,在实际开发中很容易被广大开发者忽略呢!

由于种种原因吧,开发者很不习惯在开发过程中写非常详尽的注释,而且有时候在Git提交时也是草草的一笔带过。这样做的后果就是,若干时间后,当你再次拿到这段代码,可能会花费更多的时间梳理它。

所以,善于写注释,也是提高生产效率的一种有效手段。

PHP interpreter will also ignore comments in the course of implementation, that is enough like space ratio, PHP parser will skip comments, it is responsible for executing PHP code!

PHP script comments are more diverse, there are many C-like language style, such as:

Multiline comments:

/ * This is 
Yige
multi-line
comment
/ *

Can see, multi-line comments begin with / * to * / end. C language is the same and the same, multi-line comments can not be nested.

Of course, in addition to multi-line comments, and also supports single-line comments:

echo '<p> order is processed </ p>'; // this code is PHP

Or this:

echo '<p> order is processed </ p>'; # PHP code is here

No matter what style comments taken, all the content after the comment symbol, PHP interpreter will recognize the need to deal with, and it must pay attention to it!

Guess you like

Origin www.cnblogs.com/gupiao777/p/11622479.html