PHP Basics

PHP Constants

Constants are used in a PHP project whose value never changes. Such values can be defined as constant variables. The only important point we need to remember, PHP Constant variable values can not be overwritten.Such variables are defined once and remains same as long as website exists.

PHP Constant

PHP constant can be defined using the function define.

Define function takes two argument, first one is the constant variable name and second one is the value.

Syntax

define(constantname,constantvalue)

Example:

Through out the website, the website name and home page url are going to remain same irrespective of the page a user is in.

Lets define constant variable for website name and home page url.

<?PHP

define(‘WEBSITE’,’Gyanol’);

define(‘Home’,’https://gyanol.com’);

echo WEBSITE;

echo HOME;

Output: Gyanol

Output: https://gyanol.com

This is the way we define constant in php using define function.