Home  >  Article  >  Backend Development  >  PHP basics

PHP basics

伊谢尔伦
伊谢尔伦Original
2016-12-05 10:11:334902browse

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