Home > Backend Development > PHP7 > How to define constants in PHP7.0?

How to define constants in PHP7.0?

WBOY
Release: 2023-05-26 08:42:21
Original
1685 people have browsed it

In PHP, the definition of constants can provide some specific immutable values ​​for your project. Prior to PHP 7.0, constants were defined through the define() function. However, in PHP7.0, constants can be defined using the const keyword.

This article will introduce how to define constants in PHP7.0.

Step one: Use const to define constants

Relative to the define() function, it is simpler to use the const keyword to define constants.

Let's take a look at an example:

<?php

const PI = 3.14;

echo PI;

?>
Copy after login

Output:

3.14
Copy after login

In the above example, we use the const keyword to define a constant named PI, and Its setting is 3.14. Then, we printed the value of this constant.

Please note that unlike the define() function, when defining a constant using the const keyword, you do not need to pass the string name of the constant. Instead, it defines constant names and values ​​directly after const.

Step 2: Use constants

After you define a constant, you can use it anywhere in the project. Here is a simple example:

<?php

const FRUIT = "apple";

echo FRUIT;

?>
Copy after login

Output:

apple
Copy after login

In the above example, we defined a constant named FRUIT using the const keyword and set it to "apple ". Then, we print the value of this constant on the next line of code.

Step 3: The value of the constant must be a constant expression

You must use constants in constant expressions, otherwise a compilation error will occur.

Here is an example of how to use constants in constant expressions:

<?php

const HEIGHT = 100;
const WIDTH = HEIGHT * 2;

echo WIDTH;

?>
Copy after login

Output:

200
Copy after login

In the above example, we first define the HEIGHT constant and Set it to 100. We then use the constant HEIGHT to calculate the value of the constant WIDTH, which is perfectly valid.

Step 4: Name the constant

When you use the const keyword to define a constant in PHP7.0, you must give it a name. The name must comply with the following rules:

  1. The name must start with a letter or underscore, not a number.
  2. The name can only contain letters, numbers and underscores.
  3. Names must be case sensitive.

Here are some examples of valid constant names:

const SERVER_URL = "http://example.com";
const DEBUG_MODE = true;
const MAX_USER_COUNT = 100;
Copy after login

Please note that unlike variable naming convention, in constant naming convention, spaces between words should be replaced by underscores.

Conclusion

In this article, we introduced how to define constants in PHP7.0. Defining constants using the const keyword is simpler than using the define() function. But remember, when defining a constant, its value must be a constant expression.

The above is the detailed content of How to define constants in PHP7.0?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template