PHP Introduction
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.
For 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_1 \ n ";
Echo "$ greeting_2 \ n ";
? >
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 .";
}