PHP basics

Dec 05, 2016 am 10:11 AM

1. First introduction to PHP

PHP is a server-side HTML-embedded script description language. Its most powerful and important feature is that it is cross-platform and object-oriented.

PHP is a B/S (browser/server) architecture and a three-tier structure.

1. Advantages of PHP language:

1. High security.

2. Cross-platform features.

3. Supports a wide range of databases.

4. Ease of learning.

5. Fast execution speed.

6. Free.

7. Templating.

8. Support object-oriented and process.

9. Embedded zend acceleration engine.

2. New features of PHP5:

1. Constructor and destructor.

2. Object reference.

3. Cloning of objects.

4. Private, public and protected modes in objects.

5. Interface.

6. Abstract class.

7. __call.

8. __set and __get.

9. Static members.

3. Extension libraries

Starting from PHP5, PHP has added built-in standard extension libraries, including XML extension libraries-DOM, SimpleXML, SPL, SQLite, etc., while libraries such as MySQL, mysqli, overload, gd2, etc. is placed in the pecl external extension library. in php.ini when needed. Select Load in the configuration file.

To load the extension library under Windows, you need to modify the php.ini file to complete. Users can also dynamically load through the dll() function in scripts. The DLL files of the php extension library all have the php_ prefix.

When editing the php.ini file, you should pay attention to the following points.

1. You need to modify the existence_dir setting to point to the directory where the user places the extension library or the location of the PHP_*.dll file. For example: extension_dir = C:phpextensions

2. To enable an extension library in the php.ini file, you need to remove the comment symbol before extension = php_*.dll, and delete the ";" in front of the extension library that needs to be loaded. . Such as ; extension = php_bz2.dll

3. Some extension libraries require additional dlls to work. Some of the dll files are bundled in the distribution package, but some, such as the dlls required by Oracle, are not included in the distribution package.

       

2. Formal learning php

                                                                                                                                                            ?php" and "?>" are php of tag pairs. All code within the tag pair is treated as PHP code.

echo is the output statement.

1. PHP supports a total of 4 markup styles.

(1) XML style

                                                                                                                                               " This is script style";

(3) Short style

(4) ASP style

%>

2. PHP comments support 3 types.

C++ style single-line comments

echo "C++ style"; //This is C++ style

?>

C-style multi-line comments

/*C

Style

Multi-line comments

*/

?>

shell style comments

echo "shell script style"; #The content here cannot be seen.

?>

3. PHP data types:

1. Scalar data type

The scalar data type is the most basic unit in the data structure and can only store one piece of data.

Boolean type: is the simplest type. There are only two values, true and false.

String type: A string is a continuous sequence of characters, which can be a collection of all characters that a computer can represent.

Integer type: can only contain integers. These data types can be positive or negative numbers.

Floating point: The floating point data type is used to store numbers. Unlike integers, it has decimal places.

2. Composite data types

Composite data types include two types, namely arrays and objects.

Array: A collection of variables of the same type.

Object: Object is an instance of a class and is created using the new command.

3. Special data types

Special data types include resources and null values.

Resource: Resource is a special variable, also called a handle, which is saved as a reference to an external resource. Resources are created and used through specialized functions.

                                                                                                                                                                                                                                                                                                null.

4. Convert data types

Although PHP is a weakly typed language, type conversion is still needed sometimes.

Convert to boolean type

String Convert to character type

Convert integer to integer type

Convert to floating point type

array Convert to array

Convert objcct to object

5. Detect data type

is_bool checks whether the variable is a Boolean type. For example, is_bool (true)

. is_string checks whether the variable is a string type. is_float checks whether the variable is a floating point type. is_int checks whether the variable is an integer. is_null checks whether the variable is null.

is_array checks whether the variable is an array type

is_objcct checks whether the variable is an object type

is_numeric checks whether the variable is a number or a string composed of numbers.

4. PHP constants

1. Declare and use constants

Constants can be understood as quantities whose values ​​do not change. Once a constant value is defined, it cannot be changed anywhere else in the script.

A constant consists of English letters, underscores and numbers, but numbers cannot appear as the first letter.

Use the define() function in php to define constants.

 Format define(string constant_name, mixed value, case_sensitive = true)

                                                          use’s off   out out out through out out through out out through out out out off''s' out‐‐‐‐ ‐‐‐‐‐ ,

Constant name, i.e. identifier.

                                                                                                                     whom’s who’s, The value of the constant.

case_sensitive optional parameter. Specify whether to be case sensitive. Set to true to indicate insensitivity.

There are two ways to get the value of a constant: one is to use the constant name to get the value directly; the other is to use the constant() function. The output effect of the constant() function and the direct use of the constant name are the same, but the function can Dynamically outputting different constants is much more flexible and convenient to use.

2. Predefined constants

__FILE__ Default constant, php program file name Two underscores

__LINE__ Default constant, php program line number Two underscores

PHP_VERSJON Built-in constants, version of the php program

PHP_OS Built-in constants , the name of the operating system that executes the PHP parser

                                                                                      through ’ s ’ through ’ s ` through through through ‐ ‐ ‐ ‐ ‐ , ‐ to run the php parser

                                                                                                                                                                                          A null value

     E_ERROR                          This constant points to the most recent error location

E_WARNNG This constant refers to the nearest warning

E_PARSE This constant refers to potential problems with parsing syntax

E_NOTICE This constant is for unusual occurrences A hint but not necessarily an error

5. Variable

1 , Variable declaration and use

Unlike many languages, you do not need to declare variables before using them in PHP, you only need to assign values ​​to the variables. Variable names in PHP are represented by $ and identifiers, and variable names are case-sensitive.

Variable assignment refers to giving a specific data value to a variable. For string and numeric type variables, assignment can be achieved through "=".

Variable names cannot start with numeric characters.

Variable names cannot start with characters other than letters and underscore "_".

2. Variable scope

When using variables, they must comply with the definition rules of variables. Variables must be used within a valid scope.

Local variables are variables defined inside a function, and their scope is the function where they are located.

Global variables are defined outside of magic. Their scope is the entire php file, but they are not available inside user-defined functions. If you want to use global variables inside user-defined functions, you must use global keyword declares global variables.

Static variables can retain the variable value after the function call is completed. When returning to its scope again, the original value can be continued. However, for general variables, the stored data value is cleared after the function call is completed. , the memory space occupied is also released. When using static variables, you must first declare the variable with the keyword static, and put the keyword static before the variable to be defined.

3. Variable variable

A variable variable is a unique variable that allows the name of a variable to be changed dynamically. The working principle is that the name of the variable is determined by the value of another variable. The implementation process is to add an extra dollar sign "$" in front of the variable

4. PHP predefined variables

php also provides many very practical Predefined variables, through which information such as the user session, user operating system environment, and local operating system environment can be obtained.

$_SERVER [ ' SERVER_ADDR ' ] The IP address of the server where the script is currently running

$_SERVER [ ' SERVER_NAME ' ] The name of the server host where the script is currently running. If the script is running on a virtual host, the name is determined by the value set by the virtual host.

$_SERVER [ ' REQUEST_METHOD ' ] The request method when accessing the page. Such as get, head, post, put, etc., if the request method is head, the php script will abort after outputting the header information (this means that after any output is generated, there will no longer be output buffering)

$_SERVER [ ' REMOTE_ADDR ' ] The IP address of the user who is browsing the current page

$_SERVER [ ' REMOTE_HOST ' ] The host name of the user who is browsing the current page. Reverse domain name resolution is based on the user's REMOTE_ADDR

$_SERVER [ ' REMOTE_PORT ' ] The server where the user connects to Port used

$_SERVER [ ' SCRIPT_FILENAME ' ] Absolute path name of the currently executing script.

Note that if the script is executed in the CLI as a relative path, such as file.php or .../file.php, $_SERVER [ ' SCRIPT_FILENAME' ] will contain the user-specified relative path


$_SERVER [ ' SERVER_PORT ' ] The port used by the server, the default is 80. If SSL secure connection is used, this value is the HTTP port set by the user.

$_SERVER [ ' SERVER_SIGNATURE ' ] A string containing the server version and virtual host name.

$_SERVER [ ' DOCUMENT_ROOT ' ] The document root directory where the currently running script is located. Defined in the server configuration file.

$_COOKIE Information passed to the script through HTTPCookie. Most of these cookies are set by the setcookie() function when executing the php script.

$_SESSION contains information about all session variables. The $_SESSION variable is mainly used for session control and value transfer between pages.

$_POST contains information about the parameters passed through the POST method. Mainly used to obtain data submitted through the post method.

$_GET Contains information about parameters passed through the GET method. Mainly used to obtain data submitted through the GET method.

$GLOBALS An array consisting of all defined global variables. The variable name is the index into the array. It can be called a super collection of all super variables.


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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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.

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

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.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles