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

Detailed explanation of PHP constants and data type examples

Mar 01, 2018 pm 01:45 PM
php Example Detailed explanation

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles