Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between variable and constant naming in php

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what is the difference between variable and constant naming in php. I hope you will get something after reading this article. Let's discuss it together.

Php variables and constant naming differences: 1, the variable name before the need to use the dollar sign "$" decoration; and the constant name is not preceded by the dollar sign. 2. Variable names are generally lowercase. When composed of multiple words, the first letter of the first word is lowercase and the rest is capitalized, while constant names are generally all uppercase.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What is the php constant?

The so-called constant is a quantity that cannot be changed. Once a php constant is defined, it cannot be changed or undefined. This is the official explanation and the most authoritative one. Php constants are usually used to store data that is neither changed nor desired, which can only be data of four scalar data types: integer (integer), floating point (decimal), string, and Boolean (true/false).

Naming conventions for php constants

The naming rules for php constants are similar to but not exactly the same for php variables.

Php constant names must consist of letters, underscores, and numbers; cannot start with a number; whether case sensitivity is specified at the time of definition; it is recommended to see the meaning of the name (see what it means), use English words as constant names, and recommend capitalization of all letters (e.g., PI).

The difference between php variables and constant naming:

Difference one:

The php variable name needs to be decorated with the dollar sign ($) before the variable name, while the php constant name does not.

Difference two:

Php variable names are recommended to be lowercase. When composed of multiple words, the first letter of the first word is lowercase and the rest are capitalized. Php constant names are recommended to be all uppercase.

Other differences between variables and constants:

The way it is defined is different: keywords or functions that are not defined by php variables are defined when assigned. The php constant must be defined using the define () function.

The meaning of the value is different: the value stored in the php variable can be changed, while the value stored in the php constant cannot be changed.

Scope is not used: php variables are defined outside all functions and have a global scope; when defined within a function, they have a local scope. Php constants are generally defined outside all functions and have a super-global scope.

The range of data types is different: php variables can be any data type, while php constants can only be scalar types (integer, float/double, string, boolean).

After reading this article, I believe you have a certain understanding of "what is the difference between variable and constant naming in php". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report