PHP learning book-Chapter 4

Source: Internet
Author: User
Embed PHP into HTML this chapter focuses ◆ transfer to PHP mode ◆ select the PHP 标 style ◆ Use PHP to write the first program "HelloWorld" ◆ file-included file requests after preliminary introduction in the previous chapters, now you can write PHP to embed PHP into HTML.

Highlights of this Chapter

◆ Transfer to PHP mode

◆ Select the volume label style of PHP

◆ Use PHP to write the first program "Hello World 」

◆ File inclusion request

After the preliminary introduction in the previous chapters, I believe you can write PHP scripts at the beginning. In this chapter, we will learn more about the PHP mode, the PHP volume label, and other related files, including (include) and request (require ). The reader will write the first simple PHP program from here.

HTMK is compatible with PHP

PHP can be fully mixed with HTML. In fact, PHP must be embedded in HTML. As you will see later, PHP can use some flexible syntaxes in the HTML standard, such as forms, to handle many very useful properties.

Everything that is compatible with HTML on the client is also compatible with PHP. PHP ignores other JavaScript code, calls to music and motion books, apples, or everything else on the client. PHP will ignore them, which will be passed to the client by the Web server.

In fact, you can use any method to develop Web pages, and then add the PHP program code. If you are used to working in a group and using a large multimedia drawing suite to develop every page, you can continue using this method if you want. From a practical point of view, you do not need to change the tool or workflow, just continue using the method you have been using, and add the servo end function at the end.

Jump out of HTML mode

How do we mark the PHP section in HTML? In fact, this requirement can be achieved by using special PHP labels at the beginning and end of each PHP segment. This process is called "jumping out of HTML (escaping from HTML)" or "jumping into PHP (escaping into PHP )」.

We do not want to confuse you. here, the jump (escpae) is different from the eacape used by PHP in general, which are special characters in the string (such as tab or line feed characters) previous use of backlash.

All content between the two PHP labels is identified by the PHP parser as the PHP program code. All the content beyond the two tags is irrelevant to the server, and will be directly sent to the client, which will find out what HTML, JavaScript, or other content it is.

There are four types of PHP labels. they are different when used. However, some of them are caused by personal preferences, issues that make programmers feel comfortable when using them, or settings that the previous team decided to use.

Standard PHP labels

The most commonly used PHP labels are as follows:


If this format is used, you can send a very positive label that can be correctly translated. Unless there is a good reason to select other writing formats, otherwise it is best to use this standard writing method. Any other PHP tag writing method may be canceled in the future. this is the only method considered safe.

Brief start (SGML format) label

The short start label is as follows:



This is the simplest representation. Because the number of inputs in this method is small, it will attract users who frequently switch in and out of HTML in each script. However, using this short tag also requires a price, one of the following three methods is required for PHP to identify the tag:

1. when constructing PHP, remember to select the "-- enable-short-tags" setting option.

2. enable short_open_tag settings in the php. ini file. However, when using this option, you must disable both XML and PHP, because the XML tag also uses this syntax.

There is also a way to use the short syntax: Use the short_open () function, but this usage is no longer available after PHP 4.

There are many reasons not to use the short label syntax. The main reason is that this syntax is incompatible with XML, and because XHTML is also an XML ?, So your PHP program will not be used in the XHTML file. PHP programs using short volumes are easily visually recognized. many program code color-stressed tools do not support this syntax, beginners should use the standard volume label syntax wherever possible.

In the past, many design experts used to think that PHP is commonly used. now, the PHP development team is trying to balance the standard and consistent syntax that many users can install, although there have been a large number of program codes using the old syntax in the past, because XML has become a more and more central technology for Web development, short labels will fade in the future, therefore, you must give yourself a standard syntax that is convenient to use immediately.

If you want to avoid using the short label format, remember to cancel it in your php. ini file and you will receive an error message when you forget to use the standard format.

ASP format labels

Microsoft Active Server Pages is used to hide program paragraphs and volumes in ASP format. the volumes in ASP format are as follows:



FrontPage is the most common choice for developers. To use tags in this ASP format, you must configure this option in the php. ini file. Obviously, if you use the labels in ASP format and 「. asp is an extension (which may be expected by people who previously worked on ASP websites). you need to disable ASP on the IIS server, otherwise, both program engines attempt to parse the same program section and generate unexpected results.

HTML script tag

This label is written as follows:


Although this is very efficient and can avoid FrontPage problems, it may still be very troublesome in some situations, such as the rapid emergence of php-in) variable replacement will cause trouble. In particular, if you use a lot of JavaScript on your website, be more careful, because the end script tag can easily cause confusion between the two meanings. The HTML script tag is most suitable for PHP program code when there are many differences.

The first program "Hello World 」

Now we are preparing to write the first PHP program. Open a new file in your favorite editor and enter the following content:

Each PHP block requires a PHP tag regardless of its length. Although this chapter introduces several PHP labels in writing formats, beginners should still use standard labels. You can also use the include () or require () function to include the PHP file in the file, however, the contents of files included in the file are identified as PHP program code only when they are enclosed in the PHP tag. pay special attention to this.

The above is the content in chapter 4 of the PHP learning guide. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.