Home  >  Article  >  Backend Development  >  Detailed explanation of PHP constants and data type examples

Detailed explanation of PHP constants and data type examples

小云云
小云云Original
2018-03-01 13:45:151557browse

This article mainly shares with you detailed explanations of PHP constants and data type examples, hoping to help everyone.

1. String definition methods and their differences
1. Single quotes
1.1. Single quotes cannot parse variables
1.2. Single quotes cannot parse transfer characters. They can only parse single quotes and Backslash itself
1.3, variables and variables, variables and strings, strings and strings are connected with .

2, double quotes
2.1, double quotes can parse variables, variables You can use special characters and {} to include
2.2, you can parse all transfer characters
2.3, you can also use . to connect
2.4, single quotes are more efficient than double quotes

3, heredoc and newdoc
3.1, heredoc is similar to double quotes
$str = <<...
EOT;

3.2, newdoc is similar to single quotes
$str = <<<'EOT'
...
EOT;

3.3, heredoc and newdoc both handle large text

2. Extended test points
1. Data type
1.1, scalar type
A, floating point type
cannot be used for equality judgment

B, integer

C , String

D, Boolean type
FALSE seven cases: 0, 0.0, ' ', '0', false, array(), null

1.2, Composite type
A, Array
Nine built-in super-global arrays
$_SERVER variables are set by the web server or directly associated with the execution environment of the current script
$_ENV Variables submitted by the execution environment to the script
$_GET Variables submitted to the script via URL requests
$_POST Variables submitted to the script via the http post method
$_REQUEST Variables submitted to the script via get, post and cookie mechanisms, so this array is not trustworthy
$_FILES Variables submitted to the script via http, post file upload
$_COOKIE Variables submitted to the script via the http cookies method
$_SESSION Variables currently registered for the script session
$GLOBALS Contains a reference Points to a variable that is valid in the global variable scope of each current script. The key name of this array is the name of the global variable

$_SERVER Partial field description

$_SERVER['REMOTE_ADDR'];//Display the predefined variable of the client IP
$_SERVER['REMOTE_HOST'];//Current user host name;
$_SERVER['REMOTE_PORT'];//The port used by the user to connect to the server.

$_SERVER['SERVER_ADDR'];//Display the server IP address
$_SERVER['SERVER_NAME'];//Display the server name
$_SERVER['SERVER_PORT'];// Port used by the server

$_SERVER['REQUEST_TIME'];//Display request time
$_SERVER['QUERY_STRING'];//Request query parameters

$_SERVER[' HTTP_REFERER'];//Provide the source URL, that is, anti-hotlink referer

$_SERVER['PHP_SELF'];//The file name of the executing script
$_SERVER['REQUEST_METHOD'];// Request method when accessing the page
$_SERVER['REQUEST_URI'];//Request address when accessing the page
$_SERVER['SCRIPT_FILENAME'];//Absolute path name of the currently executing script

$_SERVER['PATH_INFO'];//General framework routing uses this information


B, object

1.3, special type
A, resource
B, null

2, constant
Definition method: const, define
Once a constant is defined, it cannot be modified or deleted
2.1. The difference between const and define
A. const is faster, it is a language structure, define is a function
B. const can be used for class constant definition , but define cannot

3. Magic constant
__LINE__: Returns the current line number in the file. It can also be written as __line__.
__FILE__: Returns the absolute path of the current file (including the file name).
__DIR__: Returns the absolute path of the current file (excluding the file name), equivalent to dirname(__FILE__).
__FUNCTION__: Returns the name of the current function (or method).
__CLASS__: Returns the current class name (including the scope or namespace of the class).
__TRAIT__: Returns the current trait name (including the trait's scope or namespace).
__METHOD__: Returns the current method name (including class name).
__NAMESPACE__: Returns the name of the current file's namespace.

4. Predefined constants
Kernel predefined constants: They are constants defined in the PHP kernel. case sensitive.
PHP_VERSION: Returns the PHP version.
PHP_OS: Returns the name of the operating system where the PHP interpreter is executed.
PHP_EOL: System newline character, Windows is (\r\n), Linux is (\n), MAC is (\r).

Standard predefined constants: constants defined by PHP by default. case sensitive.
M_PI: Returns the value of pi.

5. The following predefined variables are non-global.
$php_errormsg: Previous error message, the $php_errormsg variable contains the latest error message generated by PHP. This variable is only available in the scope where the error occurred, and requires the track_errors configuration item to be turned on (it is turned off by default).
$HTTP_RAW_POST_DATA: Contains the raw data submitted by POST.

$http_response_header: HTTP response header, the $http_response_header array is similar to the get_headers() function. When using an HTTP wrapper, $http_response_header will be populated with HTTP response headers.
$argc: The number of parameters passed to the script, including the number of parameters passed to the current script when running on the command line. The file name of the script is always passed as an argument to the current script, so the minimum value of $argc is 1. This variable is only available when register_argc_argv is turned on.
$argv: Array of parameters passed to the script, containing an array of parameters passed to the current script when running on the command line. The first parameter is always the file name of the current script, so $argv[0] is the script file name. This variable is only available when register_argc_argv is turned on.

Related recommendations:

Development of code ideas for php constant usage

Summary of php constant usage

Several issues that need to be paid attention to when using PHP constants

The above is the detailed content of Detailed explanation of PHP constants and data type examples. For more information, please follow other related articles on the PHP Chinese website!

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