[PHP series tutorial] (1) -- The First PHP page

Source: Internet
Author: User
Tags phpinfo vars
Document directory
  • I. Prerequisites: Building a PHP Environment
  • Ii. First PHP page
  • 3. Practical scripts
  • 4. process the form
  • 5. Use the old PHP code in the new version of PHP

[PHP series tutorials] are collected and sorted based on the existing PHP manual, and all the code has passed the test. Indicate the source during the reprinting process.

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> <br/> <pead> <br/> <title> PHP tes </title> <br/> </pead> <br/> <body> <br /> <? PHP echo "<p> Hello World </P>" ;?> <Br/> </body> <br/> </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 <? PHP is used to indicate the start point of the PHP identifier. Then we put the PHP statement and add a termination identifier?> 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> <br/> <pead> <br/> <title> example </title> <br/> </pead> <br/> <body> <br/> <? PHP echo $ _ server ["http_user_agent"];?> <Br/> </body> <br/> </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> <br/> <pead> <br/> <title> example </title> <br/> </pead> <br/> <body> <br/> <? PHP phpinfo ();?> <Br/> </body> <br/> </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:

<? Php <br/> If (strstr ($ _ server ["http_user_agent"], "MSIE ")) {<br/> echo "you are using Internet Explorer <br/>"; <br/>}< br/>?>

The script may be displayed as follows:

You are using Internet Explorer

3. Mixed PHP and HTML Modes

<? Php <br/> If (strpos ($ _ server ["http_user_agent"], "MSIE ")! = False) {<br/>?> <Br/> <p> strpos must have returned non-false </p> <br/> <center> <B> you are using Internet Explorer </B> </center> <br/> <? Php <br/>} else {<br/>?> <Br/> <p> strpos must have returned false </p> <br/> <center> <B> you are not using Internet Explorer </B> </center> <br/> <? Php <br/>}< br/>?>

The script may be displayed as follows:

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:

<Form action = "action. PHP "method =" Post "> <br/> your name: <input type =" text "name =" name "/> <br/> your age: <input type = "text" name = "Age"/> <br/> <input type = "Submit"> <br/> </form> <br/>

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"];?>. <Br/> 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.