A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants).
Constants are case-sensitive. By convention, constant identifiers are always uppercase. The name of a constant follows the same rules as any label in PHP.
A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
Use the define() function to create a constant. It defines constant at run time. Let's see the syntax of define() function in PHP.
Syntax
define(name, value, case-insensitive)
name: It specifies the constant name.
value: It specifies the constant value.
case-insensitive: Specifies whether a constant is case-insensitive. Default value is false. It means it is case sensitive by default.
PHP introduced a keyword const to create a constant. The const keyword defines constants at compile time. It is a language construct, not a function. The constant defined using const keyword are case-sensitive.
example:
1.) Once the constant is defined, it can never be redefined. A variable can be undefined as well as redefined easily.
2.) A constant can only be defined using define() function. It cannot be defined by any simple assignment. A variable can be defined by simple assignment (=) operator.
3.) There is no need to use the dollar ($) sign before constant during the assignment. To declare a variable, always use the dollar ($) sign before the variable.
4.) Constants do not follow any variable scoping rules, and they can be defined and accessed anywhere. Variables can be declared anywhere in the program, but they follow variable scoping rules.
Constants are the variables whose values can't be changed throughout the program. The value of the variable can be changed.
Comsysapp.com is an educational website. Students and software developers can learn programming language tutorials. Comsysapp is very useful for beginners and professional developers. Comsysapp provides tutorial in easy language. Comsysapp.com has focus on simplicity.
Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.
comsysapp.com is not responsible for any mistake. We are not responsible if information made available on our website is incomplete or invalid. But comsysapp.com always try for zero-zero mistake.
comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.
copyright © 2023