Site maintenance often encountered a problem is that the structure of the site has been fixed, but in order to update a little content and have to redo a large number of pages. PHP provides a simple, efficient way to solve this problem by putting the basic structure of a Web site in a few simple HTML files (we can call it a template) , all we have to do is upload the text to the server and let the program automatically generate the Web pages according to the template, making it easier to manage large web sites.
★ Step Into PHP
If you want to create an online catalog, you will typically want to build a public design template that can be automatically generated for the framework by all pages in the directory. First, the homepage maker makes Representative HTML text, all pages of different content can be expressed by the template structure, and then processed by the program personnel. , replace the required change component of the template with a variable. Especially in database applications, using PHP, the application will extract data records from the database, and then apply templates to dynamically generate Web pages, data records will be accurately displayed in the Web page corresponding position.
If the data is a Web page content, then we can manage the website through the database. The advantage of this approach is that it eliminates repetitive content, simplifies day-to-day maintenance, and facilitates retrieval. All you have to do is paste a lot of text into the form and submit it to the database.
★php Working principle
If you create a PHP application, it's actually equivalent to creating a PHP script file. Before the server sends out the output information to the client, the network server takes the lead in processing the PHP language in the file. If your server does not support PHP, typically, the Web server will send hypertext files directly to the client's browser to indicate an answer to the HTTP request; if your suit Service supports PHP, the following processing occurs when the server responds to a request for a PHP file:
First in a PHP file, the standard HTML encoding is sent directly to the browser, while the embedded PHP program is first interpreted by the network server. If it is standard output, the output information will also be sent to the browser as standard HTML.
★php Application Example
The following is a commonly used "Hello" Demonstration Program:
< HTML >
< head >< TITLE >
My i-PHP script</title >< BODY >
<? Echo ("Hello world!"); ? >
</body >
Notice the two tags in the above document. ... ? ", this structure indicates that the PHP command will be translated by the server, rather than directly to the client. You can use or discard PHP code snippets in hypertext files based on your own design ideas. This program is very simple, you do not need PHP to create such a page, it is all about using the echo command to display "Hello world." With Echo, you can print a string with quotes, variables, or both. At the end of the echo command you can see a semicolon, and almost every PHP command ends with a semicolon.
You can watch the execution effect in the browser. Of course, we can also look at the HTML source code with View Source:
< HTML >
< head >< TITLE >
My i-php</title >< BODY >
Hello world!
</body >
We found that the HTML in the document was passed directly back, and the PHP code snippet was executed and the word "Hello world!" was typed. As you can see, the PHP code is invisible to the client, and the client sees only the result of its execution. The advantage is that it protects the source code written in PHP in the documentation, which means that the original PHP code cannot be downloaded and copied without permission.
Below we look at a use of loops to generate a Web page instance, mainly through the variable substitution of PHP to dynamically generate an HTML file (see program annotation):
< HTML >
< head >< TITLE >
PHP Font chart</title >< BODY >
< TABLE >
<?
for ($ir = 0; $ir < = 255; $ir + + 51)
{///A For loop $ir from 0 to 255 and an increment of 51
for ($ig = 0; $ig < = 255; $ig + + 51)
{///A For loop $ig from 0 to 255 and an increment of 51
? ><! --Static HTML tags can
Loops the output in the loop,
It's like print out-->
< TR >
<? for ($ib = 0; $ib <= 255; $ib + + 51)
{//A For loop
$ib from 0 to 255, and increment to 51
? >
< TD >
< FONT color= "
<?
printf ("%02x%02x%02x",
$ir, $ig, $ib);? > ">
Font # <? printf ("%02x%02x%02x",
$ir, $ig, $ib);//HTML color code? >
</font >
</td >
<? } ? >
</tr >
<? } } ? >
</table >
</body >
Performing the above program will print out a cool color table that is useful in HTML programming. Now, all browsers can view this effect, and readers can look up the generated HTML code online.