Constants in PHP are just like variables but their value can`t be change once set. It means you have to define the value of constants first and then you cannot change it.
- A perfect constant name can be start with a letter or underscore.
- Constants in PHP will automatically be global in the whole program.
Constants Syntax in PHP
The syntax of a constant in PHP is a define( ) function with some parameters which are (name, value, case-insensitive). As the name defines that the parameter of name is used for the name of constant. A value parameter is used to define the value of the constant. An important factor in constants is that they should be case-insensitive.
<!DOCTYPE html> <html> <body> <?php // case-sensitive constant name define("MESSAGE", "Welcome to our website"); echo MESSAGE; echo "<br>"; // case-insensitive constant name define("MESSAGE", "Welcome to our website", true); echo message; ?> </body> </html>
Constants Array in PHP
You can also create a constants array in PHP. This examples exactly shows it.
<!DOCTYPE html> <html> <body> <?php define("students", [ "Hamza", "Ali", "Zara" ]); echo students[0]; echo "<br>"; echo students[1]; ?> </body> </html>