Home  >  Article  >  Backend Development  >  Quick tutorial for getting started with PHP_PHP tutorial

Quick tutorial for getting started with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:22843browse

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 on 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 not unfamiliar with PHP's syntax style.
For example: echo "Hello!";
?>
The displayed 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 does not display the result Change.
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 using PHP to create an array, the user 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 multi-dimensional 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 multi-dimensional array:
$people = array ('David' => array('shirt' => 'blue','car' => 'red'),
'Adam' => array('shirt' => 'white','car' => 'silver'));
In addition, PHP also provides the built-in function count() for calculating arrays the number of elements. 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 "if" and "elseif", etc. Selective structural statements. 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 features that are easy to use. 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 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.”; }
Now, 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;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318097.htmlTechArticlePHP is a server-side scripting language used to create dynamic WEB pages. Like ASP and ColdFusion, users can mix PHP and HTML to write WEB pages. When visitors browse to the page, the service...
Statement:
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