Introduction to PHP
PHP is a server-side scripting language for creating dynamic Web pages. Like ASP and ColdFusion, users can mix PHP and HTML to write Web pages, and when a visitor browses to the page, the server first processes the PHP commands in the page and then sends the processed results along with the HTML content to the browser on the access side. However, unlike ASP or ColdFusion, PHP is a source code open program with good cross-platform compatibility. Users can run PHP on the Windows NT system and on many versions of UNIX systems, and can run PHP as a built-in module or CGI program for the Apache server.
In addition to being able to precisely control the display of Web pages, users can also send HTTP headers by using PHP. Users can set up cookies through PHP, manage user identification, and redirect users ' browsing pages. PHP has a very powerful database support capability to access almost all of the more popular database systems available today. In addition, PHP can be integrated with multiple external libraries, providing users with more useful functions, such as generating PDF files.
The user can enter the PHP command code directly in the Web page, so no special development environment is required. In the Web page, all PHP code is placed in the "< PHP" and "? > "in. In addition, users can choose to use the form < SCRIPT language= "PHP" ></script >. The PHP engine automatically recognizes and processes all the code in the page between the PHP delimiters.
The syntax structure of the PHP scripting language is very similar to the grammar style of the C and Perl languages. The variable does not need to be declared before the user can use the variable. The process of creating an array using PHP is also very simple. PHP also has the basic object-oriented component function, which can greatly facilitate the user to effectively organize and encapsulate the code they write.
PHP Syntax overview
A. Basic syntax
Even first-time users of PHP will find themselves unfamiliar with PHP's grammatical style.
For example:
<? Php
echo "hello! ”;
? >
Displays the result as "hello! ”。
In PHP, all variables begin with the identity "$". We can make the following changes to the above code:
<? Php
$greeting = "hello! ”;
Echo $greeting;
? >
The changed code shows the result unchanged.
Use "." In PHP Symbols concatenate different strings, while other arithmetic operators inherit the style of the popular programming language. Examples are as follows:
<? Php
$greeting = "hello! ”;
$num = 3 + 2;
$num + +;
echo "$greeting $num people! ”;
? >
Displays the result as "hello! 6 people! ”。
PHP has a complete set of rules for operators and operations, and if you have a C or C + + language programming background, you can find everything handy.
As with the Perl language, in PHP, if a string containing a double quotation mark contains a variable, the variable is replaced with the corresponding variable value, and if the string is enclosed in quotation marks, no substitution is made. For example:
<? Php
$name = ' PETER ';
$greeting _1 = "Hello, $name! ”;
$greeting _2 = ' Hello, $name! ';
echo "$greeting _1\n";
echo "$greeting _2\n";
? >
The results shown are:
Hello, peter!.
Hello, $name!
(Note: "\ n" in the preceding code is a newline character and can only be used under a double-quote string)
B. Variables
PHP allows users to use environment variables just as they do with regular variables. For example, the following code is included in the page http://www.nba.com/scores/index.html:
<? Php
echo "[$REQUEST _uri]";
? >
The output is [/scores/index.html]
C. Arrays
When you create an array using PHP, you can add an array index (including a regular index or associated index) to the square brackets. For example:
$fruit [0] = ' banana ';
$fruit [1] = ' Apple ';
$favorites [' animal '] = ' tiger ';
$favorites [' sports '] = ' basketball ';
If the user does not specify an array subscript when assigning a value to the array, PHP automatically adds the object to the end of the array. For example, the above $fruit array can be assigned values in the following ways, and the result remains unchanged.
$fruit [] = ' banana ';
$fruit [] = ' Apple ';
Similarly, in PHP, the user can also create multidimensional arrays as needed. For example:
$people [' David '] [' shirt '] = ' blue ';
$people [' David '] [' car '] = ' red ';
$people [' Adam '] [' shirt '] = ' white ';
$people [' Adam '] [' car '] = ' silver ';
In PHP, users can also use the array () function to quickly create an array. For example:
$fruit = Array (' banana ', ' apple ');
$favorites = Array (' Animal ' = > ' tiger ', ' sports ' = > ' basketball ');
or use the array () function to create a multidimensional array:
$people = Array (' David ' = > Array (' shirt ' = > ' blue ', ' car ' = > ' red '),
' Adam ' = > Array (' shirt ' = > ' white ', ' car ' = > ' silver '));
In addition, PHP provides built-in functions count () to calculate the number of elements in the array. For example:
$fruit = Array (' banana ', ' apple ');
Print count ($fruit);
The display result is 2.
D. Structure control
In PHP, a user can use a looping structure statement 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, the user can also use selective structure statements such as "if" and "ElseIf". For example:
if ($user _count > 200) {
Print "The site is busy right now!";}
ElseIf ($user _count > 100) {
Print "The site is active right now!";
else {
Print "The site is idle-only $user _count user logged on.";
}
http://www.bkjia.com/PHPjc/314023.html www.bkjia.com true http://www.bkjia.com/PHPjc/314023.html techarticle php Introduction PHP is a server-side scripting language for creating dynamic Web pages. Like ASP and ColdFusion, users can mix PHP and HTML to write Web pages when visitors browse to the page ...