Home > Backend Development > PHP Tutorial > PHP_Const, constphp_PHP tutorial

PHP_Const, constphp_PHP tutorial

WBOY
Release: 2016-07-12 08:59:32
Original
938 people have browsed it

PHP_Const, constphp

PHP_Const

Constant rules:
1 is always uppercase
2 A-Z and ASCII characters from 127 to 255
3 Global scope
4 Define with define function
5 Can only contain scalar data such as Boolean integer float string
6 Do not add dollar sign in front

PHP comes with constants = special constants
Case-insensitive
_LINE_ Current line number in the file
_FILE_ Full path to the file File name
_FUNCTION_ Function name
_CLASS_ Class name
_METHOD_ Class method name

_LINE_
php script line number. If a file is referenced, the constant in the referenced file is the line of the referenced file
rather than the line of the referenced file, that is, downwards The principle of passing

_FILE_
is the same as above

define part:
Macros can be used not only to replace constant values, but also to replace expressions and even code segments.

(Macros are very powerful, but they are also error-prone, so their pros and cons are quite controversial.)


The syntax of macro is:
#define Macro name and macro value
As a suggestion and a common habit among programmers, macro Names often use all capital letters.


Advantages of using macros:
1) Make the code more concise and clear
Of course, this depends on how you set the macro Pick an appropriate name. Generally speaking, macro names should have clear and intuitive meanings, and sometimes it is better to make them longer.
 2) Facilitate code maintenance
  The processing of macros is called "preprocessing" during the compilation process.

 That is to say, before formal compilation, the compiler must first replace the macros that appear in the code with their corresponding macro values. This process is a bit like the search and replace that you and I use in word processing software. Therefore, when macros are used to express constants in the code, in the final analysis, immediate numbers are used, and the type of this quantity is not clearly specified.

const part
The format of constant definition is:
const data type constant name = constant value;

A constant must be assigned a value at the beginning. Then, in subsequent code, we are not allowed to change the value of this constant.

The difference between the two:
 1 On the allocation of memory space .

 defineWhen defining a macro, no memory space will be allocated. It will be replaced in the main function during compilation. It is just a simple replacement without any checking.

 For example, type, statement structure, etc., that is, macro-defined constants are just pure placement relationships, such as #define null 0;

When the compiler encounters null, it always replaces null with 0. It has no data type (if you have any questions, please look at the preprocessing part of the C language book or look at MSDN.

 The constant defined by const has a data type,

Defining constants of data types facilitates the compiler to check the data and troubleshoot possible errors in the program,

So the difference between const and define is

Defining constants as const eliminates the unsafety between programs.

Define global constants that can be accessed anywhere

const is used for class member variable definition. It can only be accessed using the class name and cannot be changed. If you are a beginner, just understand it first and don’t get too carried away.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1097796.htmlTechArticlePHP_Const, constphp PHP_Const constant rules: 1 always uppercase 2 A-Z and ASCII characters from 127 to 255 3 Global scope 4 Use define function to define 5 which can only contain scalar data such as Boolean int...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template