PHP Constants

PHP Constants


In this post, you will learn how to use constants to store static values ​​in PHP.
What is Constant in PHP A constant is a name or an identifier for a fixed worth. Constants are like variables, actually, once they are defined, they can not be undefined or changed (with the except magic constants). Constants are very valuable for storing data that doesn't change while the script is running. Common examples of such data include configuration settings for example database username and password, site's base URL, company name, etc. Constants are defined utilizing PHP's define() function, which accepts two arguments: the name of the constant, and its value. When characterized the constant value can be accessed at any time just by referring to its name. Here is a simple example: Read also: ASP.NET VS PHP
Example
//Defining constant
define("SITE_URL", "http://cbitss.in/php-training-in-chandigarh/ ");
//Using constant
echo 'For more detail about PHP -'. SITE_URL;
?>
The output of the above code will be:
For more detail about PHP - http://cbitss.in/php-training-in-chandigarh/
PHP Echo Statement is often used to display or output data in web browsers.
Tip: By storing the value in a constant rather than a variable, you can ensure that the value won't get changed accidentally when your application runs.

Naming Conventions for PHP Constants


The name of the constant must follow the same guidelines. The variable name, which means a valid continuous name should start with a letter or underscore, followed by any number of letters, numbers or underscores with an exception:  the $ prefix isn't required for constant names.

좋은 웹페이지 즐겨찾기