Loading... In this tutorial you will learn how use constants for storing fixed values in PHP. ## What is Constant in PHP A constant is a name or an identifier for a fixed value. Constant are like variables, except that once they are defined, they cannot be undefined or changed (except [magic constants](http://www.bixiaguangnian.com/manual/php7/3995.html "magic constants")). Constants are very useful for storing data that doesn’t change while the script is running. Common examples of such data include configuration settings such as database username and password, website’s base URL, company name, etc. Constants are defined using PHP’s `define()` function, which accepts two arguments: the name of the constant, and its value. Once defined the constant value can be accessed at any time just by referring to its name. Here is a simple example: ```php <?php // Defining constant define("SITE_URL", "https://www.tutorialrepublic.com/"); // Using constant echo 'Thank you for visiting - ' . SITE_URL; ?> ``` The output of the above code will be: ```php Thank you for visiting - https://www.tutorialrepublic.com/ ``` The PHP `echo` statement is often used to display or output data to the web browser. We will learn more about this statement in the next chapter. > Tip: By storing the value in a constant instead of a variable, you can make sure that the value won’t get changed accidentally when your application runs. ## Naming Conventions for PHP Constants Name of constants must follow the same rules as [variable names](http://www.bixiaguangnian.com/manual/php7/3966.html#h2-naming-conventions-for-php-variables "variable names"), which means a valid constant name must starts with a letter or underscore, followed by any number of letters, numbers or underscores with one exception: the `$` prefix is not required for constant names. > Note: By convention, constant names are usually written in uppercase letters. This is for their easy identification and differentiation from variables in the source code. Last modification:August 27, 2024 © Allow specification reprint Like 1 如果觉得我的文章对你有用,请随意赞赏