Embed PHP into HTML
Key points of this chapter
◆ Switch to PHP mode
◆ Select PHP label style
◆ Use PHP to write the first program "Hello World"
◆ File request included in the file
After the preliminary introduction in the previous chapters, I believe you can now start writing PHP scripts. In this chapter we will learn more about PHP modes, PHP tags, and other related file includes and requirements. Readers will write their first simple PHP program from here.
HTMK is fully compatible with PHP
PHP can be completely mixed with HTML. In fact, PHP usually must be embedded in HTML. As you will see later, PHP can use some of the flexible syntax in the HTML standard, such as forms, to handle many very useful properties.
Everything that is compatible with HTML on the client side is also compatible with PHP. PHP doesn't care about other JavaScript code, calls to music and animated books, apples or anything else on the client. PHP will ignore them and the web server will pass them to the client.
In fact, readers, you can use any method to develop Web pages, and then add PHP program code to it. If you're used to working in small groups and using large multimedia drawing suites to develop each page, you can continue to do so if you wish. From a practical standpoint, you don't have to change your tools or workflow, you can just continue the way you've been doing it and add server-side functionality at the end.
Out of HTML mode
How do we mark the PHP section in HTML? In fact, this requirement can be achieved by using special PHP tags at the beginning and end of each PHP section. This process is called "escaping from HTML" or the so-called "escaping into PHP".
We don’t mean to confuse you. The escpae here is different from the eacape used in general PHP. Those are the usage of backslash before special characters (such as tab or newline characters) in the string.
All content between these two PHP tags will be recognized and understood as PHP program code by the PHP parser. All content outside these two tags has nothing to do with the server, and will be passed directly to the client, and the client will figure out whether it is HTML, JavaScript or other content.
There are four types of PHP tags, and they have different reasons for using them. But some of it is a matter of personal preference, or what the programmer feels more comfortable using, or what the previous team decided to use.
Standard PHP tags
The most commonly used PHP tags are as follows:
If you use this format, you can be very sure that the tags used can be translated correctly. Unless there is a very good reason to use another writing format, it is best to stick to this standard writing format. Any other way of writing PHP tags may be canceled in the future. This is the only way of writing that is considered safe.
Short start tag (SGML format)
The short start tag looks like this:
This is the simplest way to represent it. Since this method requires fewer inputs, it will attract users who frequently switch in and out of HTML in each script. However, there is a price to pay for using this short tag. You must do one of the following three methods to make it work. PHP recognizes this tag:
1. When building PHP, remember to select the "--enable-short-tags" setting option.
2. Enable the short_open_tag setting in the php.ini file. However, when using this option, you must prohibit the use of XML and PHP together, because XML tags also use this syntax.
There is another way to use short syntax: use the short_open() function, but this usage is no longer available after PHP 4.
There are many reasons not to use short markup syntax. The main reason is that this syntax is incompatible with XML, and because XHTML is also a kind of XML, your PHP program will not be able to be used in XHTML files. . PHP programs that use short tags are harder to visually identify. Many tools that highlight program code color do not support this syntax. Beginner designers should use standard tag syntax wherever possible.
Short tags used to be the way many design experts used to use PHP. Nowadays, the PHP development team strives to balance the standard and consistent syntax for many users to install it. Although a large number of program codes have used the old style in the past syntax, but as XML has become an increasingly central technology in web development, shorthand tags will fade away in the future, so you have to give yourself a standard syntax that's easy to use right away.
If you have made a habit of avoiding the short tag format, remember to disable it in your php.ini file, and you will respond with an error message where you forgot to use the standard format.
ASP format tags
ASP format tags are used by Microsoft Active Server Pages to hide program paragraphs and tags. The ASP format tags are as follows:
Users who use FrontPage as a development tool most often choose this format. If you want to use this ASP format tag, you must configure this option in the php.ini file. Obviously, if you use ASP format tags and ".asp" extensions (people who have worked on ASP websites before may hope so), you need to turn off the ASP function on the IIS server first, otherwise both Each programming engine will try to parse the same program paragraph and produce unpredictable results.
HTML script tag
This tag is written as follows:
Although this is very efficient and can avoid FrontPage problems, it can still be very troublesome in some cases, such as when popping up quickly. You will encounter trouble when replacing variables (php-in). In particular, if you use a lot of JavaScript on your website, you must be more careful, because the closing script tag can easily cause confusion between the two meanings. The HTML script tag is best used when the PHP program code is very different.
The first program "Hello World"
Now we are ready to write the first PHP program. First open a new file in your favorite editor and type the following:
In most browsers, except for the PHP part, other content is not very necessary. However, you should still get into the habit of using standard HTML structures and embedding PHP.
If you don’t see output very similar to the one shown in Figure 4-1, then there is a problem. It is likely that there is a problem with some installation or settings.
Review the content of Chapter 3 to understand the instructions on Anxi, and read back to Chapter 37 to find related settings and configuration options. In addition, some common problems are analyzed in Chapter 15 and information about error problems is not provided.
In and out of PHP mode
At any time in the PHP script, you may be in PHP mode, or in HTML outside PHP mode. There is no other intermediate state here. All content within the PHP tag is PHP program code, and all content outside the tag is ordinary HTML content.
You can use the PHP tag to switch to PHP mode at any time as you like, for example:
Please note what happens in the first PHP mode instance. There is a number specifying the value here. When it comes to the second PHP This designation of program code is still valid. In the next chapter you'll learn more about the use of variables when entering and exiting PHP mode. In Chapter 14, you will also learn about different styles of using PHP patterns.
File include
Another way to add PHP to HTML is to put the PHP program code into a separate file and then call it using PHP's include function. There are four related include functions:
In the previous version of PHP, the include and require functions had significant differences in function and speed. This will no longer be true. These two types of functions only The types of error messages thrown when an error occurs are different. The include() function and include_once() function will only issue a warning when an error occurs, but the require() function and require_once() function will issue a serious error and interrupt the program. .
As mentioned in the naming of the functions, include_once() and require_once() differ from include() and require() in that they only allow the file to be included only once in each PHP program. This Extremely useful when you include files containing PHP functions. Because redefining the function will automatically cause a serious error. In large PHP systems, it is often necessary to include files that themselves include other files, but it is difficult to track whether you have included a specific file before, but once you use include_once() or require_once() This doesn't need to be the case.
How do you decide whether to use the include() function? Basically you have to decide whether to stick with the program you wrote and avoid the pain of serious errors, or whether to make common errors in your program without access. The most serious way is to use require(), which will be used in your program. When it is not perfect, it is an interrupt program. The least rigorous one is include_once(), which will naturally hide some of your bad programming habits.
The most common PHP include function is to add the homepage and footer to all pages of the website.
For example, there is now a simple header file (called header.inc) as shown below:
Obviously, this single change simplifies the maintenance and scale of the entire website. Now if you want to use a different visual presentation or if you want to update the copyright notice, you only need to change one file instead of changing the same HTML page of several pages. Program fragments.
When you include files, remember to set the include_path directive correctly in your php.ini file. By using the correct directives you can include files from sites higher up the site or outside of your web structure. , see Chapter 37 for more details.
As you can see in the above program, PHP’s include() function will only send the included files in text form. Many people think that because the include() function is used in PHP mode, It is not true that the included part will also belong to PHP mode. In fact, the server will jump back to HTML mode at the beginning of the included file, and quietly return to PHP mode after the included file ends, and immediately combine it with the semicolon .
You must always use PHP start and end tags when you want to use PHP programs. Any included files must add legal tags if they want to be executed with PHP. If the entire file belongs to the PHP part ( Commonly found in files using functions), the entire file must be wrapped in PHP tags.
Now let’s look at the file below, database.inc:
We can’t fully elaborate here: if you have problems including PHP files, especially if the results you see are not what you expected or expected. If you don't see what you should see, be absolutely careful to include PHP tags before and after you include the file.
If you are stupid and only include the above fragment file into the PHP program, you will see that this database variable will become text and displayed on the page. Because you neglected to use the PHP tag, the parser thought that this paragraph was HTML. , the correct way to write database.inc should be as follows:
For all files that include other files, you must make sure not to use a newline on the last line. Remember that anything outside of PHP is considered an HTML part, even The same goes for a blank line. Blank lines or whitespace characters outside of PHP tags will be output. If you include this file when output is not available, such as before using HTTP headers, your program will display a big For the error message that the output stream is already used in an included file, see Chapter 15 for more details.
Abstract
PHP is easily embedded into HTML. You can use any HTML generation method you are accustomed to, and then add PHP program code sections to it. The added PHP program code section can be a single number with only a little comment, or it can be a very long program code section.
Every PHP block, no matter how long or short it is, must have a PHP tag. Although this chapter introduces PHP tags in several writing formats, beginners should still use the standard way of writing tags. You can also use the include() or require() function to include the PHP file in the file, but the content of the included file will only be recognized as PHP program code if it is enclosed in a PHP tag. Pay special attention.
The above is the content of Chapter 4 of the PHP Learning Guide. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!