Sunday, 24 July 2022

Constant in PHP

What is Constant in PHP

A constant is a name or an identifier for a fixed value. Constants are like variables, except that once they are defined, they cannot be undefined or changed.

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. 

<?php // Defining constant define("SITE_URL", "https://www.tutorialrepublic.com/"); // Using constant echo 'Thank you for visiting - ' . SITE_URL; ?>


Conventions for PHP Constants

Name of constants must follow the same rules as variable names, which means a valid constant name must start with a letter or underscore, followed by any number of letters, numbers, or underscores with one exception: the $ the prefix is not required for constant names.






No comments:

Post a Comment

Microsoft Thwarts Chinese Cyber Attack Targeting Western European Governments

  Microsoft on Tuesday   revealed   that it repelled a cyber attack staged by a Chinese nation-state actor targeting two dozen organizations...