Home Backend Development PHP Tutorial Detailed explanation of the definition and usage examples of PHP constants

Detailed explanation of the definition and usage examples of PHP constants

Apr 27, 2017 pm 05:10 PM

What are constants?

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.

How to define constants

Constants are composed of English letters, underscores, and numbers. However, one thing to note is that the first letter of our constants cannot be numbers. started.

In PHP we use the define() function to define constants. The syntax of the define() function is as follows

bool define ($name , $value ,$case_insensitive = false)
Copy after login

Detailed explanation of parameters

This function has three Parameters:

$name: The name of the constant, must have

#$value: The value of the constant, must have

$case_insensitive Used to control constants Whether the name is case-sensitive (the default is sensitive), set true to indicate insensitivity. Optional parameters, there are two values, true and false

Get the constant

There are two ways to get the value of the constant: one is to use the constant name to get the value directly

<?php
header("content-type:text/html;charset=utf-8");              //设置字符编码
define(&#39;name&#39;,&#39;PHP中文网&#39;);
echo name;
?>
Copy after login

The other is to use the constant() function. The constant() function has the same effect as directly using the constant name to output, but the function can dynamically output different constants, which is much more flexible and convenient to use.

The syntax format of the constant() function is as follows

constant (const_name )
Copy after login

The parameter const_name is the name of the constant to be obtained, or it can be a variable that stores the constant name. If the acquisition is successful, the value of the constant is returned, otherwise an error message is displayed. The constant is not defined.

<?php
header("content-type:text/html;charset=utf-8");              //设置字符编码
define("MAXSIZE", &#39;PHP中文网&#39;);

echo constant("MAXSIZE");
?>
Copy after login

Code running results:

Detailed explanation of the definition and usage examples of PHP constants

Determine whether the constant is defined

To determine a constant Whether it has been defined, you can use the defined() function. The syntax of the function is as follows

defined(name)
Copy after login

Parameters name is the name of the constant to be obtained. It returns true if successful, otherwise it returns false.

<?php
header("content-type:text/html;charset=utf-8");              //设置字符编码
define("GREETING","PHP中文网");
echo defined("GREETING");
?>
Copy after login

Code running results:

Detailed explanation of the definition and usage examples of PHP constants

Example

For more information To better understand how to define constants, here is an example of defining constants. Use the above-mentioned define() function, constant() function, and defined() function in the example. Use the define() function to define a constant, use the constant() function to dynamically obtain the value of the constant, and use the defined() function to determine whether the constant is defined. The example code is as follows

<?php
header("content-type:text/html;charset=utf-8");              //设置字符编码
define("GREETING","看到PHP中文网一次");                      

echo GREETING."<br/>";                                       //输出常量GREETING
echo Greeting ."<br/>";

define(&#39;COUNT&#39;,&#39;能看到PHP中文网多次&#39;,true);
echo COUNT ."<br/>";                                        //输出常量COUNT
echo Count ."<br/>";                                        //输出常量COUNT,因为设定了大小写不敏感

$name=&#39;count&#39;;
echo constant($name)."<br/>";                               //输出常量COUNT
echo(defined(&#39;GREETING&#39;))."<br/>";                          //如果常量被定义,则返回true,使用echo输出显示1

?>
Copy after login

The result of running the code:

Detailed explanation of the definition and usage examples of PHP constants

The above is a simple example where we define, obtain and judge constants through functions. The next section, We explain, "Predefined constants" in PHP.

Recommended related video tutorials: "php.cn Dugu Jiujian (4) - PHP video tutorial": Variables and constants: small warehouse for storing data in the program.

The above is the detailed content of Detailed explanation of the definition and usage examples of PHP constants. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are constants in C language? Can you give an example? What are constants in C language? Can you give an example? Aug 28, 2023 pm 10:45 PM

A constant is also called a variable and once defined, its value does not change during the execution of the program. Therefore, we can declare a variable as a constant referencing a fixed value. It is also called text. Constants must be defined using the Const keyword. Syntax The syntax of constants used in C programming language is as follows - consttypeVariableName; (or) consttype*VariableName; Different types of constants The different types of constants used in C programming language are as follows: Integer constants - For example: 1,0,34, 4567 Floating point constants - Example: 0.0, 156.89, 23.456 Octal and Hexadecimal constants - Example: Hex: 0x2a, 0xaa.. Octal

How to create a constant in Python? How to create a constant in Python? Aug 29, 2023 pm 05:17 PM

Constants and variables are used to store data values ​​in programming. A variable usually refers to a value that can change over time. A constant is a type of variable whose value cannot be changed during program execution. There are only six built-in constants available in Python, they are False, True, None, NotImplemented, Ellipsis(...) and __debug__. Apart from these constants, Python does not have any built-in data types to store constant values. Example An example of a constant is demonstrated below - False=100 outputs SyntaxError:cannotassigntoFalseFalse is a built-in constant in Python that is used to store boolean values

iOS 17: How to change iPhone clock style in standby mode iOS 17: How to change iPhone clock style in standby mode Sep 10, 2023 pm 09:21 PM

Standby is a lock screen mode that activates when the iPhone is plugged into the charger and oriented in horizontal (or landscape) orientation. It consists of three different screens, one of which is displayed full screen time. Read on to learn how to change the style of your clock. StandBy's third screen displays times and dates in various themes that you can swipe vertically. Some themes also display additional information, such as temperature or next alarm. If you hold down any clock, you can switch between different themes, including Digital, Analog, World, Solar, and Floating. Float displays the time in large bubble numbers in customizable colors, Solar has a more standard font with a sun flare design in different colors, and World displays the world by highlighting

What is the definition of short video? What is the definition of short video? Dec 23, 2020 pm 02:56 PM

The definition of short video refers to high-frequency pushed video content that is played on various new media platforms, suitable for viewing on the move and in a short-term leisure state, and is generally spread on new Internet media within 5 minutes. Video; the content combines skills sharing, humor, fashion trends, social hot spots, street interviews, public welfare education, advertising creativity, business customization and other themes. Short videos have the characteristics of simple production process, low production threshold, and strong participation.

The definition and function of MySQL composite primary key The definition and function of MySQL composite primary key Mar 15, 2024 pm 05:18 PM

The composite primary key in MySQL refers to the primary key composed of multiple fields in the table, which is used to uniquely identify each record. Unlike a single primary key, a composite primary key is formed by combining the values ​​of multiple fields. When creating a table, you can define a composite primary key by specifying multiple fields as primary keys. In order to demonstrate the definition and function of composite primary keys, we first create a table named users, which contains three fields: id, username and email, where id is an auto-incrementing primary key and user

In Java, is it possible to define a constant using only the final keyword? In Java, is it possible to define a constant using only the final keyword? Sep 20, 2023 pm 04:17 PM

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

What is Discuz? Definition and function introduction of Discuz What is Discuz? Definition and function introduction of Discuz Mar 03, 2024 am 10:33 AM

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

How to make custom borders in Microsoft Word How to make custom borders in Microsoft Word Nov 18, 2023 pm 11:17 PM

Want to make the front page of your school project look exciting? Nothing makes it stand out from other submissions like a nice, elegant border on the homepage of your workbook. However, the standard single-line borders in Microsoft Word have become very obvious and boring. Therefore, we show you the steps to create and use custom borders in Microsoft Word documents. How to Make Custom Borders in Microsoft Word Creating custom borders is very easy. However, you will need a boundary. Step 1 – Download Custom Borders There are tons of free borders on the internet. We have downloaded a border like this. Step 1 – Search the Internet for custom borders. Alternatively, you can go to clipping

See all articles