Home > Backend Development > PHP Tutorial > Chengdu University of Technology College of Engineering and Technology Academic Affairs Office PHP Getting Started Crash Course

Chengdu University of Technology College of Engineering and Technology Academic Affairs Office PHP Getting Started Crash Course

WBOY
Release: 2016-07-29 08:36:47
Original
1877 people have browsed it

PHP is a server-side scripting language used to create dynamic WEB pages. Like ASP and ColdFusion, users can use a mixture of PHP and HTML to write WEB pages. When a visitor browses to the page, the server will first process the PHP commands in the page, and then transmit the processed results together with the HTML content to Access browser. But unlike ASP or ColdFusion, PHP is an open source 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 of the Apache server or as a CGI program.
In addition to accurately controlling the display content of WEB pages, users can also send HTTP headers by using PHP. Users can set cookies through PHP, manage user identification, and redirect users' browsing pages. PHP has very powerful database support functions and can access almost all currently 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.
Users can enter PHP command codes directly into the WEB page, so no special development environment is required. In WEB pages, all PHP code is placed in "". In addition, users can also choose to use forms such as , etc. The PHP engine will automatically identify and process all code between PHP delimiters in the page.
 The grammatical structure of PHP scripting language is very similar to the grammatical style of C language and Perl language. Users do not need to declare variables before using them. The process of creating an array using PHP is also very simple. PHP also has basic object-oriented component functions, which can greatly facilitate users to effectively organize and encapsulate the code they write.
 
Overview of PHP syntax
A. Basic syntax
Even users who are new to PHP will find that they are no stranger to PHP’s grammatical style.
For example: echo "Hello!";
?>
The display result is "Hello!".
In PHP, all variables start with the identifier "$". We can make the following changes to the above code:
$greeting = “Hello!”;
echo $greeting;
?>
The changed code will display the same result.
PHP uses the "." symbol to connect different strings, while other arithmetic operators inherit the style of popular programming languages. For example:
$greeting = “Hello!”;
$num = 3 + 2;
$num++;
echo “$greeting $num people!”;
?>
The displayed result is “Hello” ! 6 people!”
  PHP has a complete set of regulations for various operators and operation rules. If the user has a programming background in C or C++ language, he will find that everything is handy.
  Like the Perl language, in PHP, if a variable is contained in a string enclosed in double quotes, the variable will be replaced with the corresponding variable value; if the string is enclosed in single quotes, no replacement will be performed. For example:
$name = ‘PETER’;
$greeting_1 = “Hello, $name!”;
$greeting_2 = ‘Hello, $name! ';
echo "$greeting_1n";
echo "$greeting_2n";
?>
The displayed result is:
Hello, PETER!
Hello, $name!
(Note: "n" in the above code is a newline character and can only be used in double-quoted strings)
 
B. Variables
PHP allows users to use environment variables like regular variables. For example, the page http://www.nba.com/scores/index.html contains the following code:
echo "[$REQUEST_URI]";
?>
The output result is [/scores /index.html]
 
C. Array
When users create an array using PHP, they can add the array index (including regular index or associated index) in square brackets. For example:
$fruit[0] = 'banana';
$fruit[1] = 'apple';
$favorites['animal'] = 'tiger';
$favorites['sports'] = 'basketball';
 If the user does not specify the array subscript when assigning a value to the array, PHP will automatically add the object to the end of the array. For example, the above $fruit array can be assigned in the following way while keeping the result unchanged,
$fruit[] = 'banana';
$fruit[] = 'apple';
Similarly, in PHP, users can also create as needed Multidimensional Arrays. 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 array() The function creates a multidimensional array:
$people = array ('David' => array('shirt' => 'blue','car' => 'red'),
'Adam' => array(' shirt' => 'white','car' => 'silver'));
In addition, PHP also provides a built-in function count() for counting the number of elements in an array. For example:
$fruit = array(‘banana’, ‘apple’);
print count($fruit);
The displayed result is 2.
 
D. Structure control
In PHP, users 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 return 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, users can also use selective structural 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 many powerful and easy-to-use functions. In terms of form processing, PHP can automatically assign the data sent by the client form to the corresponding variables, thus greatly simplifying the entire form processing process.
For example, the user creates the following form:

When using PHP to process the above code, PHP automatically creates a variable named $name , and assign the variable value "PETER" to the variable.
Users can directly perform various operations on variables created by PHP, such as displaying variable values:
echo "Hi $name!";
or verifying variable values:
if ($name == "PETER") { echo " Please check out your email.”; }
Next, let’s take a look at how to create and process a basic form using PHP. In the example, we will ask the form filler to answer a few short questions, including entering the form filler's name, email address, completing a questionnaire, etc.
 Here, we divide the page we want to design into two functional modules: form display and form processing. In this way, users do not have to design two different pages, but only need to display or process the corresponding functional modules through logical control in the same PHP page.
 
A. Form display
We implement the display of the form through the first functional module. The specific code is:
function display_form()
{
global $PHP_SELF;
?>
 

”METHOD=GET>
Name:
Favorite Fruit: Apple
Orange
Banana
Favorite Times to Eat Fruit:
Morning
Noon
Dinner
Latenight



 
}
?>
  Most of the above codes are the HTML codes necessary to create the form. Here, we only give 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. The variable $PHP_SELF is a very convenient pointer in PHP, and its variable value is the URL address of the current page. In this way, we can achieve the purpose of processing the form by the form page 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 we write without having to worry about re-entering the new page address after each change. .
 In addition, when using the $PHP_SELF variable, we adopt the following method:
global $PHP_SELF;
This indicates that the $PHP_SELF variable in the code is a global variable. When using PHP, users must note that any 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. Even if a variable with the same name exists outside the function, the value of the variable is different. Therefore, if we do not explicitly declare the function variable $PHP_SELF as a global variable with a globally unique value in the code, the user will find that the value of the variable $PHP_SELF in the function will be empty, not what we expected in advance. The URL address of the current page.
 Users may have noticed that we set the name of the form check option to a times[] array, and set the name of the radio option to a fruit regular variable. This is because the radio option only allows the user to select one correct option, so the value of the fruit variable can only be a string; on the contrary, the check option allows the user to make multiple choices. If the user wants PHP to save all the choices the user may make, he or she needs to use an array to save all possible values. According to PHP's syntax rules, we add square brackets after the variable name times to let PHP know that the variable is an array variable rather than an ordinary regular variable.
Finally, we set an implicit variable called stage. By using this variable, we can control whether to display the form or process the form results.

The above introduces the PHP introductory quick tutorial of the Academic Affairs Office of the College of Engineering and Technology of Chengdu University of Technology, including the content of the Academic Affairs Office of the College of Engineering and Technology of Chengdu University of Technology. I hope it will be helpful to friends who are interested in PHP tutorials.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template