Home >Backend Development >PHP Tutorial >Quick introduction to PHP (1)_PHP tutorial

Quick introduction to PHP (1)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:11:01849browse


Introduction to PHP

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 "< ?php" and "? >". Additionally, users can choose to use forms such as < SCRIPT LANGUAGE="php" >< /SCRIPT >. The PHP engine will automatically identify and process all code between PHP delimiters in the page.

The grammatical structure of the PHP scripting language is very similar to the grammatical style of the 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:

< ? php

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:
< ? php

$greeting = "Hello!";

echo $greeting;

? >

The changed code shows the same result.
PHP uses the "." symbol to connect different strings, while other arithmetic operators inherit the style of popular programming languages. For example:

< ? php

$greeting = “Hello!”;

$num = 3 + 2;

$num++;

echo “$greeting $num people !";

? >

The 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:

< ? php

$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 Use environment variables just like regular variables. For example, the page http://www.nba.com/scores/index.html contains the following code:

< ? php

echo “[$REQUEST_URI]”;

? >

The output result is [/scores/index.html]



C. Array

When users create an array using PHP, they can Enclose array indexes (either regular or associative) 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 counting the number of elements in an array. For example:

$fruit = array('banana', 'apple');

print count($fruit);

The result is 2.



D. Structural 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 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, 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.”;

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314023.htmlTechArticle Introduction to PHP PHP 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...
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