[PHP series tutorial] (1) The first PHP page

Source: Internet
Author: User
I. Prerequisites: the setup of the PHP environment has been described in detail. The website is blog. csdn. netrocket5725archive200912285090489. aspx. 2. The first PHP page is under the root directory of the WEB server D: ProgramFilesApacheSoftwareFoundationApache2.2htdocs

I. Prerequisites: the setup of the PHP environment has been described in detail at http://blog.csdn.net/rocket5725/archive/2009/12/28/5090489.aspx. 2. The first PHP page is under the root directory of the WEB server D:/Program Files/Apache Software Foundation/Apache2.2/htdocs

I. Prerequisites: Building a PHP Environment

The environment is described in detail at http://blog.csdn.net/rocket5725/archive/2009/12/28/5090489.aspx.

Ii. First PHP page

Create a file named Hello. php under D:/Program Files/Apache Software Foundation/Apache2.2/htdocs under the root directory of the WEB server, and then complete the following.

<Html> <pead> <title> PHP Tes </title> </pead> <body> <? Php echo "<p> Hello World </p>" ;?> </Body> </ptml>

Enter a URL in the address bar of the browser to access the file, and add "/hello. php" at the end ". If you develop it locally, the URL is generally http: // localhost/hello. php or http: // 127.0.0.1/hello. php. Of course, this depends on the WEB server settings. (The Apache configuration file is httpd. conf ). If all the settings are correct, the file will be parsed by PHP, And the browser will output the following result: Hello World
This program is very simple. It only uses the echo () Statement of PHP to display Hello World. You will not be satisfied with this.

The above example aims to display the format of the PHP special identifier. In this example, we use To exit the PHP mode. You can enable or disable the PHP mode in the HTML file as needed.

3. Practical scripts

Now let's write some more practical scripts. We will check what type of browser visitors use to browse the page. To achieve this goal, we need to check the user's agent string, which is part of the HTTP request sent by the browser. This information is stored in a variable. In PHP, variables always start with a dollar sign. The variable we are interested in now is $ _ SERVER ["HTTP_USER_AGENT"].

1. to display the variable, perform the following operations:

<Html> <pead> <title> Example </title> </pead> <body> <? Php echo $ _ SERVER ["HTTP_USER_AGENT"];?> </Body> </ptml>

The output of this script may be:

Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)

PHP has many different types of variables. In the preceding example, an array element is printed. Arrays are a type of very useful variables.

2. Use phpinfo () to display all predefined Variables

<Html> <pead> <title> Example </title> </pead> <body> <? Php phpinfo ();?> </Body> </ptml>

If you open this file in your browser, you will see the complete information about PHP and all the variables you can use.
You can add multiple PHP statements to a PHP identifier, or create a code block to do more things than simple echo. For example, if we need to check Internet Explorer, we can perform the following operations:

<? Phpif (strstr ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE") {echo "You are using Internet Explorer <br/>" ;}?>

The script may be displayed as follows:

You are using Internet Explorer

3. Mixed PHP and HTML Modes

<? Phpif (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE ")! = False) {?> Strpos must have returned non-false <center> <B> You are using Internet Explorer </B> </center> <? Php} else {?> Strpos must have returned false <center> <B> You are not using Internet Explorer </B> </center> <? Php }?>

The script may be displayed as follows:

strpos must have returned no-false
 
  You are using Internet Explorer
 

Different from the above echo statement in PHP, we jumped out of the PHP mode to directly write HTML code. It is worth noting that the logic efficiency of scripts is the same for both cases. After determining whether the strpos () function returns TRUE or FALSE, that is, whether the string MSIE is found, only one HTML block is sent to the viewer.

4. process the form

A useful feature of PHP is its processing of PHP forms. What you need to understand is that any element of the form automatically takes effect in your PHP script.

The following is a simple HTML form:

There is nothing special in this form, and no special identifier is used. When the user fills out the form and clicks the submit button, the page action. php will be called. In this file, you can add the following content to print the content from the form:

Hi <? Php echo $ _ POST ["name"];?>. You are <? Php echo $ _ POST ["age"];?> Years old.

The output of this script may be:

Hi Xue-Jamy.You are 25 years old.

The script should have done a lot of work, and there is no more complicated content here. PHP automatically sets the $ _ POST ["name"] and $ _ POST ["age"] variables for you. Before that, we used the automatic global variable $ _ SERVER. Now we have introduced the automatic global variable $ _ POST, which contains all the POST data. Note the method used to submit data in the form ). If we can use the GET method, the information in the form will be stored in the automatic global variable $ _ GET. If you do not care about the source of the REQUEST data, you can also use the automatic global variable $ _ REQUEST, which contains all GET, POST, COOKIE, and FILE data.

5. Use the old PHP code in the new version of PHP

Now, PHP has developed into a popular scripting language. You can find the code that you can reuse in your own scripts in many public resources. Developers of the PHP language have made a lot of effort for downward compatibility. Therefore, in the new version of PHP, the code of the old version should be able to run without any changes (ideally. But in fact, we still have to make some changes to the Old Code.
The two most important changes that may affect the code of earlier versions are:

(1) The old $ HTTP _ * _ VARS array is canceled (they are originally global variables in functions or methods ). PHP 4.1.0 introduces the following automatic Global Array variables: $ _ GET, $ _ POST, $ _ COOKIE, $ _ SERVER, $ _ FILE, $ _ ENV, $ _ REQUEST, and $ _ SESSION. The old $ HTTP _ * _ VARS arrays, such as $ HTTP_POST_VARS, have been used since PHP 3 and they still exist. In PHP 5.0.0, you can set register_long_arrays to block long PHP pre-defined arrays.

(2) external variables are no longer registered as global variables by default. That is, from PHP 4.2.0, the default value of register_globals in $ php. ini; is off. We recommend that you use the automatic Global Array variable mentioned above to access these values. However, the old scripts, books, and tutorials may all be based on the on setting. If this option is set to on, you can go to the URL http://www.example.com/foo.php? $ Id is directly used in id = 42. However, whether set to on or off, $ _ GET ['id'] is always valid.

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.