Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set constants by php

Shulou Source: shulou.com Published: 2022-06-03 14:02:59 10月27日 Update

This article will explain in detail how to set constants for php. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Php set constant method: 1, use define () function, syntax "define ('constant name', 'constant value')"; 2, use const keyword, syntax "const constant name = constant value;"

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The so-called constant is a quantity that cannot be changed. Once a constant is defined in PHP, it cannot be modified or undefined.

PHP constants are usually used to store data that is neither changed nor desired, which can only be data of four scalar data types: integer, floating-point, string, and Boolean, but the constant supports array since PHP7.

The method of setting a constant

1. Use the define () function

Legal constant names can only start with letters and underscores, followed by letters, numbers, or underscores of any length. You can define a constant through the define () function in PHP. The syntax of the define () function is as follows:

Define (string $name, mixed $value [, bool $case_insensitive = false])

The arguments to the define () function are described in the following table:

Parameter description $name required parameter, constant name (need to be wrapped in double quotes or single quotation marks) $value required parameter, constant value $case_insensitive optional parameter, specify whether it is case-sensitive. Default is case-sensitive. Setting it to true means case-insensitive.

2. Use the const keyword

In addition to using the define () function, you can also use the const keyword to define constants. The syntax for the const keyword is as follows:

Const constant name = constant value

Here is an example to demonstrate the use of constants:

This is the end of the article on "how to set constants in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Constant function parameter key keyword data syntax name size article underscore letter quotation mark method more type good legal practical content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux OPPO Reno NVidia MariaDB