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

Quick introduction to PHP (1)_PHP tutorial

Jul 21, 2016 pm 04:11 PM
asp php web getting Started create dynamic yes Serve end Introduction Script language quick fix


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles