search
HomeBackend DevelopmentPHP7How to define constants in PHP7.0?

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;

?>

Output:

3.14

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;

?>

Output:

apple

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;

?>

Output:

200

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;

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!

Statement
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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)