PHP Quick Start (1)

Source: Internet
Author: User
Tags php introduction


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 isSource codeOpenProgramAnd has 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 on the web page.CodeTherefore, no special development environment is required. 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 popularProgramming Language. 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 .";

}

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.