We already learned about variables. I assume you would have a clear idea about variables. As we know the value of a variable can be changed numerous times throughout the program. but on the other hand value of constants can not change. Constants are also used to store data like variables but once constants get initialized values can not be changed.
With the help of constants, we have stored data that would not change in the whole program for example store data like DB_username, DB_password, or BASE_URL in constants. Constants help us to make sure that our data would not change throughout the whole program even if not by mistake by the programmer.
If you define a constant in a program the only way to change the value is to change the value where you define the constant for the very first time in the program.
In PHP we can define constants using the define() function. This function accepts two parameters first one is the name of the constant and the second is the value of the constant. After defining the constant you can access it anywhere in the program by using its name. Please check the following example program to understand constants more clearly.
<?php
//defining constant Here
define("language", "PHP");
//accessing constant with name
echo 'I Love ' . language;
?>
Parvesh Sandila is a results-driven tech professional with 8+ years of experience in web and mobile development, leadership, and emerging technologies.
After completing his Master’s in Computer Applications (MCA), he began his journey as a programming mentor, guiding 100+ students and helping them build strong foundations in coding. In 2019, he founded Owlbuddy.com, a platform dedicated to providing free, high-quality programming tutorials for aspiring developers.
He then transitioned into a full-time programmer, where his hands-on expertise and problem-solving skills led him to grow into a Team Lead and Technical Project Manager, successfully delivering scalable web and mobile solutions. Today, he works with advanced technologies such as AI systems, RAG architectures, and modern digital solutions, while also collaborating through a strategic partnership with Technobae (UK) to build next-generation products.
