How to set constants in php: 1. Use the define() function, the syntax "define('constant name', 'constant value')"; 2. Use the const keyword, the syntax "const constant name = constant value;".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The so-called constant is a quantity that cannot be changed, in PHP Once a constant is defined, it cannot be modified or undefined.
PHP constants are usually used to store data that does not change and is not expected to change. The data can only be data of four scalar data types: integer, floating point, string, and Boolean. However, Starting from PHP7, constants support array types.
How to set constants
1. Use the define() function
Legal constants A name can only begin with a letter and an underscore, and can be followed by any length of letters, numbers, or underscores. In PHP, you can define a constant through the define() function. The syntax of the define() function is as follows:
define(string $name, mixed $value [, bool $case_insensitive = false])
define() The description of the function parameters is as shown in the following table:
Parameters | Description |
---|---|
$name | Required parameter, constant name (needs to be wrapped in double quotes or single quotes) |
$value | Required parameter, constant value |
$case_insensitive | Optional parameter, Specify whether to be case-sensitive. The default is case-sensitive. If set to true, it 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 of the const keyword is as follows:
const 常量名 = 常量值;
The following uses examples to demonstrate the use of constants:
<?php header("Content-type:text/html;charset=utf-8"); define('WebSite', 'php中文网'); const url = 'https://www.php.cn/'; echo WebSite.'<br>'; echo url; ?>
Running results:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to set constants in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
