Home  >  Article  >  Backend Development  >  Learn how to use constants in php

Learn how to use constants in php

WBOY
WBOYOriginal
2016-07-25 09:04:48922browse
In PHP, constant refers to a value that cannot be modified during program execution. When dealing with values ​​that absolutely do not need to be modified, consider setting them to constants.

In PHP, constant refers to a value that cannot be modified during program execution. When dealing with values ​​that absolutely do not need to be modified, consider setting them to constants. For example: how many feet is one mile equivalent to (5280)? You can set this value as a constant. Once a constant is defined, it cannot be modified anywhere in the program.

In php, we use the define() function to define constants. The define() function defines a constant by assigning a value to a variable name. Its form is as follows: boolean define(string name, mixed value, [, bool case_insensitive])

If the optional parameter case_insensitive is used when defining, and the value of this parameter is set to true, the reference to this constant in the subsequent program will not be case-sensitive.

Example, define the numerical constant PI: Learn how to use constants in php The above code will produce the following output: The value of pi is 3.1415926. Pi doubled equals 6.283184.

Note: 1) There is no need to use the dollar sign $ in front of the constant reference. 2) Once a constant is defined, it cannot be redefined or retrieved. If you need to generate a value based on an existing constant, this value must be stored in another variable. 3). Constants are global and can be referenced anywhere in the php script.

Finally, let’s look at how to detect whether a constant is defined. Use defined in php to check whether a constant exists. Example 2: Learn how to use constants in php Okay, that’s it for the basic usage of constants in PHP. Hope this helps you have a clear understanding of PHP constants. Programmer's home, dedicated to you.



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