PHP QuickStart Quick _php Tutorial

Source: Internet
Author: User
Tags arithmetic operators
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 " In In addition, users can choose to use forms such as the. 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 _1n";
echo "$greeting _2n";
? >
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.";

}

 


Form processing

PHP provides users with a number of easy-to-use and powerful features. In the form of processing, PHP can automatically assign the data sent by the client form to the corresponding variable, which greatly simplifies the process of the entire form.

For example, the user creates the following form:

When using PHP to process the above code, PHP automatically creates a variable named $name and assigns the variable value "PETER" to the variable.

Users can perform various actions directly on the variables created by PHP, such as displaying variable values:

echo "Hi $name! ”;

Or to validate the value of a variable:

if ($name = = "PETER") {echo "Please check out your email." ; }

Let's take a look at how to create and process a basic form by using PHP. In the example, we will ask the form's fill-in to answer a few short questions, including the name of the person who entered the form, the e-mail address, the completed questionnaire, and so on.

Here, we will design the page according to function divided into form display and form processing two functional modules. In this way, users do not have to design two different pages, but only in the same PHP page through the logical control to display or process the corresponding function module.

 

A. Form Display

We use the first function module to realize the display of a form. The specific code is:

<? Php

function Display_form ()

{

Global $PHP _self;

? >

 

 

}

?>

The vast majority of the code above is the necessary HTML code to create the form. Here, we only have 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. Variable $php_self is a handy pointer in PHP whose variable value is the URL address of the current page. This allows us to implement the form's purpose by setting the value of target to $php_self in the subsequent form tag. 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 page code that is written without worrying about re-entering the new page address after each change.

In addition, when using the $php_self variable, we used the following method:

Global $PHP _self;

This indicates that the $php_self variable in the code is a global variable. In the process of using PHP, users must be aware that any function variables in PHP are local variables. This means that any

http://www.bkjia.com/PHPjc/532508.html www.bkjia.com true http://www.bkjia.com/PHPjc/532508.html techarticle 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 visitors browse to the page, the service ...

  • 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.