How to declare constants in php?
Constants can be understood as quantities whose values do not change. Once a constant is defined, it cannot be changed anywhere else in the program script.
You can use the define() function to define constants. After PHP 5.3.0, you can use the const keyword to define constants outside the class definition. Once a constant is defined, it cannot be changed or undefined.
Constants can only contain scalar data ( boolean , integer , float and string ). You can define resource constants, but they should be avoided as they can cause unpredictable results.
You can get the value of a constant simply by specifying its name. Unlike variables, you should not add the $ sign in front of a constant. If the constant name is dynamic, you can also use the function constant() to get the value of the constant. Use get_defined_constants() to get a list of all defined constants.
Note: Constants and (global) variables are in different namespaces. This means that for example TRUE and $TRUE are different.
If an undefined constant is used, PHP assumes that what is wanted is the name of the constant itself, as if calling it with a string (CONSTANT corresponds to "CONSTANT"). An E_NOTICE level error will be issued. See the manual for why $foo[bar] is wrong (unless bar is previously defined as a constant using define() ). If you just want to check whether a certain constant is defined, use the defined() function.
Example #1 Define constants
<?php define ( "CONSTANT" , "Hello world." ); echo CONSTANT ; // outputs "Hello world." echo Constant ; // 输出 "Constant" 并发出一个提示级别错误信息 ?>
Example #2 Use the keyword const to define constants
<?php // 以下代码在 PHP 5.3.0 后可以正常工作 const CONSTANT = 'Hello World' ; echo CONSTANT ; ?>
Note:
And use define() to define constants In contrast, constants defined using the const keyword must be in the topmost scope because they are defined at compile time. This means that you cannot use const to define constants inside functions, loops, and if statements.
The above is the detailed content of How to declare 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
