PHP Constants Explained: Definition, Types and Examples

PHP Constants Explained: Definition, Types and Examples

PHP lo website or web application develop chestunnappudu different types of data ni variables lo store chestam. User name, email, age, price, product details lanti values program requirement batti change avvachu. Kani konni values matram application motham lo same ga undali. Alanti values ni repeatedly change cheyakunda fixed ga maintain cheyadaniki PHP lo constants ni use chestam.

Constant ante oka fixed value. Oka constant ki value assign chesina tarvata normal program execution lo aa value ni change cheyadam possible kaadu. For example, website name, company name, tax rate, application version or a fixed configuration value lanti information ni constant ga store cheyochu.
PHP lo constants chala useful because same value ni application lo different places lo use cheyalsina situation lo value ni oka place lo define chesi, required places lo reuse cheyochu. Dini valla code maintain cheyadam easy avutundi.

PHP lo constants create cheyadaniki mainly define() function mariyu const keyword use chestam. Rendu methods tho constants create cheyagaligina, vatini declare chese way and konni usage differences untayi.

Ee article lo PHP constants ante enti, constants ni enduku use chestam, define() function tho constant ela create cheyali, const keyword ela use cheyali, constants and variables madhya difference enti, naming rules enti ane topics ni simple examples tho telusukundam.

PHP coding manam oka website ki leka application ki lopala em process jaragali ani chupisthundi. Manam Facebook anukunte, andulo PHP coding use chestharu. Andulo manaki login ID, name, photo, postlu chupinchadaniki PHP vadatharu. Appudu server ki ardham avuthundi. PHP coding tho user ki em chupinchalo adi server lo save avuthundi.

Manam PHP ani pilusthamu kani asalu name Hypertext Preprocessor. PHP full form idi. PHP ni easy ga nerchukovachu, mariyu website build cheyadaniki kuda speed ga cheyavachu.

Constant in php:

Once we assigned any value in constant we cannot change that value in execution of php code .
There  are two types of constants:

1.define

PHP lo define() function use chesi constant create cheyochu.
define() function lo constant name and value ni provide chestam.
Syntax for define()
define(“CONSTANT_NAME”, “value”);

Ikkada:

CONSTANT_NAME → Constant name
value → Constant lo store cheyalsina value

Example 1: Creating a Constant Using define()

<?php

define(“SITE_NAME”, “OneSiddharth”);

echo SITE_NAME;

?>

Output:

OneSiddharth

Explanation

Ee program lo define() function use chesi SITE_NAME ane constant create chesam.
Daani value OneSiddharth.
Tarvata echo SITE_NAME; use chesi constant value ni webpage lo display chesam.
Constant name ni quotes lopala define chestam, kani use chestunnappudu constant name ni $ symbol lekunda use chestam.
Variables ki $ symbol use chestam:
$name = “Siddharth”;
Kani constants ni generally ila use chestam:
SITE_NAME
Idi variables and constants madhya important difference.

Example 2: Using a Constant Multiple Times

<?php

define(“SITE_NAME”, “OneSiddharth”);

echo “Welcome to ” . SITE_NAME . “<br>”;
echo “Visit ” . SITE_NAME . ” for useful tutorials.”;

?>

Output:

Welcome to OneSiddharth
Visit OneSiddharth for useful tutorials.

Explanation

Ee example lo SITE_NAME constant ni okasari define chesi, program lo rendu different places lo use chesam.
Constant value same ga maintain avvadam valla same information ni multiple places lo easy ga reuse cheyochu.

2.const

Define():
Define is used in functions.so, that we are assigning a variable values for identifiers.these define function working at runtime.
Define function is easy and flexible for the users.
Syntax for constant
define(“identifier”, “variable _value”);

Program:

<?php
define(“welcome”, “one siddharth.co.in”);
echo welcome.”\n”;
//This a case sensitive constant.
define(“Welcome”,”To the php world”,true);
echo welcome;
//This is not a case sensitive constant.
Differences is we are using true on the define function
?>
Output:
one siddharth.co.in
To the php world.
Explanation:-
Php code lo manam define function use chesi constant code rasam.aa define function kooda okati case Sensitive constant and inkokato case sensitive kaadu.rendu Ela use cheyalo differences ento php comment use chesi rasamu.

3.const

const is another type of define function.but the difference is here we are using const keyword.
This constant function is used at compile time not in runtime of the php code.
These are only case sensitive.

Syntax for const:

const Constant_name=”value”;
Program for const using php:
<?php
const Variable=”hello friends”;
echo Variable.”\n”;
const Abc=”Welcome To the php world”;
echo Abc;
//This is case sensitive.so,we are writing constant name exactly
?>
Output:-
hello friends
Welcome To the php world
Explanation:
Php lo constant anedi inko keyword .manam deenni const ane danitho start chestam paina ichina syntax use chesi code rastamu.akkada \n ivvatam valan output anedi next line loki velthundi.
Constant name em aithe istamo adhe echo lo print cheyamani adagali.case sensitive kabatti letter small or capital edhi iste adhe ivvali.

Conclusion

PHP constants anedi application lo fixed values ni store and reuse cheyadaniki chala useful feature. Oka value program lo multiple places lo same ga undali anukunnappudu constant ni create chesi required places lo reuse cheyochu.

PHP lo constants create cheyadaniki define() function and const keyword ane two common methods unnayi. define() function syntax lo constant name and value ni provide chestam. const keyword tho direct ga constant declaration rayachu.

Constants ni website name, company name, application version, configuration values and other fixed information kosam use cheyochu. Variables tho compare cheste constants fixed values ni represent chestayi, while variables values ni change cheyadaniki allow chestayi.

Beginners munduga define() and const methods tho simple constants create chesi practice cheyali. Tarvata constants ni real-world PHP applications lo website settings and configuration values tho use cheyadam practice chesthe concept inka clear ga artham avutundi.

2 thoughts on “PHP Constants Explained: Definition, Types and Examples”

Leave a Comment