PHP is a computer programming language that is mainly used to develop Web applications (website construction, etc.). In this series of blogs, we introduce the basic knowledge of PHP from the basic syntax of PHP. Make readers learn the fun of programming in a simple way.

This series of blog posts will cover the following nine topics: variables, constants, data types, operators, arrays, flow control (order, selection, loop), functions, file handling, and object orientation

Each article will leave a homework at the end of the article, the answer to get please private chat me, we can actively discuss in the comment section, progress together

The concept of constant

Constants, as the name implies, are immutable quantities whose value cannot be changed throughout the execution of PHP code.

Constants are named the same way as variables, but traditionally constant identifiers are always uppercase. Constants are declared without the $sign.

Beginners may think that both variables and constants represent the same value, so why distinguish between variables and constants? In fact, variables and constants are different responsibilities, in real development will be based on different use scenarios to choose the appropriate variables and constants.

How to declare constants

  • The define () function

To define a constant, use PHP’s built-in define(), with the first argument being the constant name (uppercase) and the second the value of the constant.

<? PHP /** * Created by */ define("NAME"," Lengyue "); // lengyue is the value echo NAME; / / output lengyue
  • Use the const keyword

Simply add the keyword const before declaring a constant.

<? PHP /** * Created by String */ const NAME = "Lengyue "; echo NAME;

The code field

<? PHP /** * Created by */ define("NAME"," Lengyue "); // define() echo NAME; const AGE = 18; // use const to declare the constant AGE echo AGE;

Practice after class

  1. Two ways to declare constants?
  2. What’s the naming convention for constants?

Practice your answers after class and do a WeChat search.
The senior of the”Reply
phpTo obtain


The article continues to be updated on GitHub
https://github.com/lengyueit/phpFamilyThis has been included. Welcome STAR.

If this post is helpful to you, please give Lengyue a like or add a follow!