PHP quick start tutorial

Source: Internet
Author: User

PHP is a server scripting language used to create dynamic web pages. Like ASP and ColdFusion, users can use PHP and HTML to write WEB pages. When a visitor browses this page, the server first processes the PHP commands on the page, then, the processed results and HTML content are sent to the browser at the access end. However, unlike ASP or ColdFusion, PHP is an open source code program with good cross-platform compatibility. You can run PHP on Windows NT and many versions of Unix systems, and run PHP as a built-in module of the Apache server or CGI program.
In addition to precisely controlling the content displayed on the web page, you can also use PHP to send HTTP headers. Users can set cookies through PHP, manage user identification, and redirect user browsing pages. PHP has very powerful database support functions and can access almost all of the more popular database systems. In addition, PHP can be integrated with multiple external libraries to provide users with more practical functions, such as generating PDF files.
You can directly enter the PHP Command code on the WEB page without any special development environment. On the WEB page, all PHP code is placed in "<? Php "and"?>" . In addition, you can also select a format such as <script language = "php"> </SCRIPT>. The PHP engine automatically identifies and processes all code on the page between PHP delimiters.
The syntax structure of PHP script is very similar to that of C and Perl. You do not need to declare variables before using them. Using PHP to create an array is also very simple. PHP also has basic object-oriented component functions, which greatly facilitates users to effectively organize and encapsulate their own code.
 
PHP syntax Overview
A. Basic syntax
Even users who are new to PHP will find that they are familiar with the syntax style of PHP.
Example: <? Php
Echo "Hello !";
?>
The result is "Hello !".
In PHP, all variables start with "$. We can make the following changes to the above Code:
<? Php
$ Greeting = "Hello !";
Echo $ greeting;
?>
The result of the changed code remains unchanged.
PHP uses the "." symbol to connect different strings, while other Arithmetic Operators inherit the style of popular programming languages. Example:
<? Php
$ Greeting = "Hello !";
$ Num = 3 + 2;
$ Num ++;
Echo "$ greeting $ num people !";
?>
The result is "Hello! 6 people !".
PHP has a complete set of rules for various operators and operation rules. If you have a C or C ++ programming background, you can find everything is handy.
Like Perl, in PHP, if a string contained in double quotation marks contains a variable, the variable will be replaced with the corresponding variable value; if the string is included in single quotation marks, no replacement will be made. For example:
<? Php
$ Name = 'Peter ';
$ Greeting_1 = "Hello, $ name !";
$ Greeting_2 = 'hello, $ name! ';
Echo "$ greeting_1n ";
Echo "$ greeting_2n ";
?>
The result is as follows:
Hello, PETER!
Hello, $ name!
(Note: "n" in the above Code is a line break and can only be used under double quotation marks)
 
B. Variables
PHP allows users to use environment variables like regular variables. For example, the following code is included in http://www.nba.com/scores/index.html:
<? Php
Echo "[$ REQUEST_URI]";
?>
The output result is [/scores/index.html].
 
C. Array
When using PHP to create an array, you can add the array index (including the regular index or associated index) to square brackets. For example:
$ Fruit [0] = 'bana ';
$ Fruit [1] = 'apple ';
$ Favorites ['animal '] = 'tiger ';
$ Favorites ['Sports '] = 'bucketball ';
If you do not specify an array subscript when assigning values to an array, PHP automatically adds the object to the end of the array. For example, you can assign values to the above $ fruit array to keep the results unchanged,
$ Fruit [] = 'bana ';
$ Fruit [] = 'apple ';
Similarly, in PHP, you can create multi-dimensional arrays as needed. For example:
$ People ['David'] ['shirt'] = 'blue ';
$ People ['David'] ['car'] = 'red ';
$ People ['Adam '] ['shirt'] = 'white ';
$ People ['Adam '] ['car'] = 'sil ';
In PHP, you can also use the array () function to quickly create an array. For example:
$ Fruit = array ('banana ', 'apple ');
$ Favorites = array ('animal '=> 'tiger', 'Sports '=> 'bucketball ');
Or use the array () function to create a multi-dimensional array:
$ People = array ('David' => array ('shirt' => 'blue', 'card' => 'red '),
'Adam '=> array ('shirt' => 'white', 'cart' => 'sil '));
In addition, PHP provides the built-in function count () to calculate the number of elements in the array. For example:
$ Fruit = array ('banana ', 'apple ');
Print count ($ fruit );
The result is 2.
 
D. Structure Control
In PHP, you can use loop structure statements such as "for" or "while. For example:
For ($ I = 4; $ I <8; $ I ++ ){
Print "I have eaten $ I apples today. n ";}
Or
$ I = 4; while ($ I <8 ){
Print "I have eaten $ I apples today. n ";
$ I ++;
}
The returned result is:
I have eaten 4 apples today.
I have eaten 5 apples today.
I have eaten 6 apples today.
I have eaten 7 apples today.
In addition, you can use Selective structure statements such as "if" and "elseif. For example:
If ($ user_count> 200 ){
Print "The site is busy right now !";}
Else'if ($ user_count> 100 ){
Print "The site is active right now !";
Else {
Print "The site is idle-only $ user_count user logged on .";
}
 
Form Processing
PHP provides many easy-to-use and powerful functions for users. In terms of form processing, PHP can automatically assign values to corresponding variables for the data sent by the client form, which greatly simplifies the process of form processing.
For example, the user creates the following form:
<Input type = text name = "name" VALUE = "PETER">
When PHP is used to process the above Code, PHP automatically creates a variable named $ name and assigns the variable value "PETER" to the variable.
You can directly perform various operations on the variables created in PHP, such as displaying the variable value:
Echo "Hi $ name !";
Or verify the variable value:
If ($ name = "PETER") {echo "Please check out your email .";}
Next, let's take a look at how to create and process a basic form using PHP. In the example, we will ask the form writer to answer a few short questions, including the name, email address, and questionnaire of the input form writer.
Here, we divide the page to be designed into two functional modules: Form display and form processing. In this way, users do not have to design two different pages, but only need to display or process the corresponding functional modules in the same PHP page through logical control.
 
A. Form display
We use the first function module to display forms. The Code is as follows:
<? Php
Function display_form ()
{
Global $ PHP_SELF;
?>
 
<Form target = "<? Php echo $ PHP_SELF;?>" METHOD = GET>
Name: <input type = text name = "name"> <br> Favorite Fruit: <input type = radio name = "fruit" VALUE = "apple"> Apple
<Input type = radio name = "fruit" VALUE = "orange"> Orange
<Input type = radio name = "fruit" VALUE = "banana"> Banana
Favorite Times to Eat Fruit:
<Input type = checkbox name = "times []" VALUE = "m"> Morning
<Input type = checkbox name = "times []" VALUE = "n"> Noon
<Input type = checkbox name = "times []" VALUE = "d"> Dinner
<Input type = checkbox name = "times []" VALUE = "l"> Latenight
<Input type = hidden name = "stage" VALUE = "results">
<Input type = submit value = "Thanks!">
</FORM>
 
<? Php
}
?>
Most of the above Code is the HTML code necessary to create a form. Here, we will only give a brief introduction to the PHP knowledge involved.
First, let's take a look at the variable $ PHP_SELF at the beginning of the Code. The variable $ PHP_SELF is a convenient pointer in PHP, and its value is the URL address of the current page. In this way, we can set the TARGET value to $ PHP_SELF in the subsequent form tag to process the form from the form page. Here, we use the $ PHP_SELF variable instead of the actual address of the page, because by using the $ PHP_SELF variable, we can easily modify and move the compiled Page code, you don't have to worry about entering a new page address after each change.
In addition, when using the $ PHP_SELF variable, we adopt the following method:
Global $ PHP_SELF;
This indicates that the $ PHP_SELF variable in the Code is a global variable. When using PHP, you must note that any function variable in PHP is a local variable. This means that the scope of any function variable is limited to the function that includes the variable. Even if a variable with the same name exists outside the function, the value of the variable is different. Therefore, if we do not explicitly declare the function variable $ PHP_SELF in the Code as a global variable with a globally unique value, the user will find that the value of the variable $ PHP_SELF in the function will be null, rather than the URL address of the current page we expected in advance.
You may have noticed that we set the name of the option selected in the form to the times [] array, and set the name of the option selected to the regular variable fruit. This is because the single choice option only allows the user to select the only correct option, so the value of the fruit variable may only be a string; on the contrary, the check option allows the user to make multiple choices. If you want PHP to save all possible options, you need to use an array to save all possible values. According to the PHP syntax rules, we add square brackets after the variable name times to let PHP know that the variable is an array variable rather than a common variable.
Finally, we set an implicit variable named stage. By using this variable, we can control whether to display the form or process the form result.

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.