In PHP, constants are used to store values that cannot be modified. You can define constants using the define() function, specifying the constant's name and value, and optionally choosing whether it is case-sensitive or not.
Defining a constant is very simple; you just need to use the define() function.
define(name, value, [case-insensitive]);
define('PI', 3.14159265); // Define a float constant
define('TAX_RATE', 0.08); // Define a numeric constant
define('COMPANY_NAME', 'Acme Corp.'); // Define a string constant
You can directly access a defined constant by its name:
echo PI; // Output 3.14159265
echo TAX_RATE; // Output 0.08
echo COMPANY_NAME; // Output Acme Corp.
By mastering PHP constants, you can handle values that do not need to change, improving the maintainability and readability of your code.