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:
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 constantdefine(“identifier”, “variable _value”);
Program:
3.const
Syntax for const:
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”