PHP Introductory Crash Tutorials _php Basics

Source: Internet
Author: User
Tags arithmetic operators arrays php and php code
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 delivers the processed results along with the HTML content to the browser on the access side. But unlike ASP or ColdFusion, PHP is a source-code-open program with good cross-platform compatibility. Users can run PHP on Windows NT systems and many versions of UNIX systems, and can run PHP as a built-in module or CGI program for Apache servers.
In addition to being able to control the display content of Web pages accurately, users can also send HTTP headers by using PHP. Users can set cookies through PHP, manage user identification, and redirect users to browse the page. PHP has a very powerful database support capability to access almost all of the more popular database systems at the moment. In addition, PHP can be integrated with multiple external libraries to provide users with more practical functions, such as the production of PDF files.
Users can enter the PHP command code directly on the Web page, and therefore do not need any special development environment. In Web pages, all PHP code is placed in "<?php" and "?>". In addition, users can also choose to use a form such as <script language= "PHP" ></SCRIPT>. The PHP engine automatically identifies and processes all the code in the page between the PHP delimiters.
The syntax structure of the PHP scripting language is very similar to that of the C and Perl languages. Users do not need to declare a variable before they use it. The process of creating arrays using PHP is also very simple. PHP also has the basic object-oriented component function, can greatly facilitate the user to effectively organize and encapsulate the code that they write.

PHP Syntax overview
A. Basic grammar
Even users who are first in touch with PHP will find that they are not unfamiliar with PHP's grammatical style.
For example: <?php
echo "hello! ”;
?>
Display 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 same result.
In PHP, use the "." Symbols connect different strings, while other arithmetic operators inherit the style of popular programming languages. Examples are as follows:
<?php
$greeting = "hello! ”;
$num = 3 + 2;
$num + +;
echo "$greeting $num people! ”;
?>
Display result as "hello!" 6 people! ”。
PHP has a complete set of operators and Operation rules, if the user has a C or C + + language programming background, you can find everything is handy.
As in Perl, in PHP, if you include a variable in a string containing a double quote, 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";
?>
Display results as:
Hello, peter!.
Hello, $name!
(Note: "N" in the above 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 result is [/scores/index.html]

C. Array
When you create an array using PHP, you can add an array index (including a regular index or associated index) to 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 values to an array, PHP automatically joins the object to the end of the array. For example, the above $fruit array can be assigned in the following way to keep the result unchanged.
$fruit [] = ' banana ';
$fruit [] = ' Apple ';
Similarly, in PHP, users can also build 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 build 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 a built-in function count () to calculate the number of elements in an array. For example:
$fruit = Array (' banana ', ' apple ');
Print count ($fruit);
The display result is 2.

D. Structural control
In PHP, a user can use a loop 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 result returned 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 the 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 variety of powerful features that are easy to use. In terms of the processing of forms, PHP can automatically assign values sent by the client form to the corresponding variables, greatly simplifying the process of the entire form.
For example, the user establishes the following form:
<input type=text name= "NAME" value= "PETER" >
When you use 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 variables created by PHP, such as displaying variable values:
echo "Hi $name! ”;
or verify the value of the variable:
if ($name = = "PETER") {echo "Please check out your email." ; }
Next, let's take a look at how to create and process a basic form by using PHP. In the example, we will ask the person who filled out the form to answer a few short questions, including the name of the person who filled in the form, the e-mail address, the questionnaire, and so on.
Here, we will design the page according to the 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 logical control to display or process the corresponding functional modules.

A. The form display
We use the first functional module to implement the display of the form. The specific code is:
<?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 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
}
?>
The vast majority of the above code is the HTML code necessary to create the form. Here, we only have a brief introduction to the knowledge of PHP involved.
First, let's look at the variable $php_self at the beginning of the code. Variable $php_self is a handy pointer in PHP, and its variable value is the URL address of the current page. In this way, we can implement the purpose of the form page to process the form by setting the target value to $php_self in the following 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 written, without having to worry about entering the new page address after each change.
In addition, when using the $php_self variable, we use the following methods:
Global $PHP _self;
This indicates that the $php_self variable in the code is a global variable. Users in the process of using PHP must be aware that any of the function variables in PHP are local variables. This means that the scope of any function variable is limited to the function itself that contains the variable. The value of a variable is not the same even if there is a variable with the same name outside the function. Therefore, if we do not explicitly declare the function variable $php_self as a global unique value in the code, the user will find that the value of the variable $php_self in the function will be empty, rather than the URL address of the current page that we expected beforehand.
The user may have noticed that we set the name of the form check option to the times[] array, and the name of the radio option is set to the fruit general variable. This is because the radio option only allows the user to select the only correct option, so the value of the fruit variable can only be a string; instead, the check option allows the user to make multiple selections. If the user wants PHP to be able to save all the choices that a user might make, you need to use an array to hold all of the possible values. In accordance with the syntax rules of PHP, we add square brackets after the variable name times so that PHP knows that the variable is an array variable rather than a regular variable.
Finally, we set up an implied variable called stage, which allows us to control whether or not to display the form or to process the form results.
Related Article

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.