MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Complete Tutorials of PHP OOP Constants with Example code

In this Tutorial, we are going to learn about Complete Tutorials of PHP OOP Constants with Example code. Through this we will learn about the concept about Constants.  But first let me give you a brief explanation of what is Object-oriented programming (OOP)?

What is Object-oriented programming (OOP)?

Object-oriented programming consists of combining a set of variables (properties) and functions (methods), which are referred to as an object. These things are arranged into classes in which individual items can be combined. OOP can enable you to consider the objects and the many activities in connection with the objects in a program’s code. For more details you can go on my previous article Read More. [Object-oriented programming (OOP) Concept Simplified!]

What is Constants in PHP?

Constant Types-PHP

A constant is a variable that cannot be modified, exactly as the name indicates. If you define a constant, you assign it a value, and the value never changes thereafter. Simple variables are normally easier to operate, but, in certain situations, constants, such as signals to other programmers (or yourself, should you forget), are preferred not to modify this particular value while running.

Class constants are similar to normal constants, except that they are defined in a class and accessible through this particular class. You use the double-colon operator to access a constant class, as with static members.

A class constant is declared inside a class with the const keyword.

We can access a constant from outside the class by using the class name followed by the scope resolution operator (::) followed by the constant name,

Which are the points we have to know about the Constants?

PHP constants | PHP Constants Types | How to Define a Constant in php
  • Constants are one variable type that we may classify with const for any class.
  • The value cannot be modified after assignment of these variables.
  • Class constants are distinct from conventional variables, as the class constants do not require $ to declare.
  • If we are inside the class then values of the constants can be get using self keyword, but accessing the value outside the class you have to use Scope Resolution Operator.
  • A particular entity which is fixed on a class basis.
  • Constant can also be used for interfaces.
  • The class name can truly be a variable when a constant of a class is called using the ($)classname (::) syntax.

How to create a PHP Constants?

Php constants class 336

The define() function in PHP is used to create a constant

<span class="has-inline-color has-vivid-red-color"><code>define(name, value, case_insensitive)</code></span>Code language: HTML, XML (xml)

There are some of The parameters are as follows:-

  • name: The name of the constant.
  • value: The value to be stored in the constant.
  • case_insensitive: Defines whether a constant is case insensitive. By default this value is False, i.e., case sensitive.
<code><span class="has-inline-color has-vivid-red-color"><strong>define("PI",3.14);</strong></span></code>Code language: HTML, XML (xml)

The constant is specified as a key value pair in the above programmer. The value of the constant is obtained with the key.

In the program above, just two parameters are provided to define(). Optionally, the define() provides a third parameter to set a constant’s case insensitivity. The constants are sensitive to cases by default.

Example:-

Output:-

PHP define() constant example output printing GeeksforGeeks twice

The constant() function in PHP

Another technique to output constants using the constant() function is not to use the echo statement.

PHP constant() is employed as an input for this method to retrieve the value of the constant. The sample code below use constant() to obtain the constant value by using the identification.

<code><span class="has-inline-color has-vivid-red-color"><strong>constant(name)</strong></span></code>Code language: HTML, XML (xml)

Example:-

Output:-

PHP constant() function example output printing GeeksforGeeks twice

PHP Constants with const

A construct in which define() is a function is a PHP const. In order to initialize constants, we need use the static name and value with a const statement where define() permits interpolation and function calling while declaring the constant. This constriction is always case sensitive, specified in this construct.

Setting constants with const is beneficial in the aspect of readability; nevertheless, define() has several functions, which enables case sensitivity, allowing function call values to be assigned to the constant, which reveals constants at runtime.

The following line illustrates a const statement as an example of initialization.

const PI = "3.14";
echo PI; // Output: 3.14Code language: PHP (php)

Some Examples!

Basic of PHP

Example 1:-

Output:-

PHP class constant example 1 output: Thank you for visiting W3Schools.com!

Example 2:-

Output:-

PHP constant example 2 output showing the value 10

Example 3:-

Output:-

PHP constant example 3 output: SELECT * FROM users and SELECT * FROM roles

Conclusion

Use the const keyword to define a class constant. Use the visibility modifier keywords, including public, protected, and private. By default, a class constant is public. we have learnt many basic concept of Constant concept in PHP. Hope you have Enjoyed this. Thank you.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
👤 About the Author <strong>Ashwani</strong> is passionate about DevOps, DevSecOps, SRE, MLOps, and AiOps, with a strong drive to simplify and scale modern IT operations. Through continuous learning and sharing, Ashwani helps organizations and engineers adopt best practices for automation, security, reliability, and AI-driven operations. 🌐 Connect & Follow: <strong>Website:</strong> <a href="https://www.wizbrand.com/">WizBrand.com</a> <strong>Facebook:</strong> <a href="https://www.facebook.com/DevOpsSchool">facebook.com/DevOpsSchool</a> <strong>X (Twitter):</strong> <a href="https://x.com/DevOpsSchools">x.com/DevOpsSchools</a> <strong>LinkedIn:</strong> <a href="https://www.linkedin.com/company/devopsschool">linkedin.com/company/devopsschool</a> <strong>YouTube:</strong> <a href="https://www.youtube.com/@TheDevOpsSchool">youtube.com/@TheDevOpsSchool</a> <strong>Instagram:</strong> <a href="https://www.instagram.com/devopsschool/">instagram.com/devopsschool</a> <strong>Quora:</strong> <a href="https://devopsschool.quora.com/">devopsschool.quora.com</a> <strong>Email</strong>- contact@devopsschool.com

Related Posts

Top 10 ORM (Object Relational Mapping) Tools in 2026: Features, Pros, Cons & Comparison

Introduction In the world of software development, managing data between object-oriented programs and relational databases can be a complex task. This is where Object Relational Mapping (ORM)…

Read More

Top 50 Scala Interview Questions And Answers

Sure, here are some top 50 Scala interview questions and answers with headings: 1. What is Scala? Scala is a general-purpose programming language that combines object-oriented and…

Read More

What is C++ and use cases of C++?

What is C++? C++ is a high-level programming language that was developed by Bjarne Stroustrup in 1983 at Bell Labs. It is an extension of the C…

Read More

What is Java and use cases of Java?

What is Java? Java is a high-level programming language originally developed by Sun Microsystems, now owned by Oracle Corporation. It is designed for creating applications that run…

Read More

Python Tutorials: Class & Object

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

What is Java and How it works? An Overview and Its Use Cases

What is java? Java is a high-level, class-based, object-oriented programming language.It is a simple programming language. Java makes writing, compiling,and debugging programming easy. It helps to create…

Read More