Free cookie consent management tool by TermsFeed Generator PHP Constant | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Constant

PHP Constant

Last Updated on Mar 22, 2023

What is a constant?

Constants are like variables that can’t be changed. Hence the name! it’s constant and it’s not variable.

You can define a constant by using the keyword const and you don’t need the dollar sign ($) in your name and even when you want to use it you don’t need the $

we normally write the name of the constant all uppercase

const NAME= 'amir';
echo NAME;
// amir

After you define the constant if you try to change it you’ll get an error

const NAME= 'amir';
NAME= 'pratham';
// error

You can also have constants inside your class

class Example{
    const MESSAGE = 'I am a constant inside a class';
}

Constants inside the class act like static parameters.

inside the class we get access to them with self and outside the class we access them with ::

class Example{
    const MESSAGE = 'I am a constant inside a class';
    public function getMesasge(){
        echo self::MESSAGE;
    }
}

https://youtu.be/YEm7NvXxFUo

Conclusion

Now you know about constants in PHP.

I recommend you to open a PHP files and define multiple constants. compare them to variables.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what are constants
  • how to define a constant
  • how to use constants
  • constant in class

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Namespace
Mar 23, 2023 programming

PHP Namespace

Today we are going to talk about namespaces in PHP. Namespace is like putting the classes in a group. it helps you organize your code much better. It's a must-know topic for a PHP programmer. ...

7 Min Read Read More
PHP array change key case
Feb 14, 2023 programming

PHP array change key case

Today we are going to talk about changing the case of the keys of an array in PHP. Sometimes you might change the case of the keys in your array. It's very easy to do that in PHP. ...

4 Min Read Read More
PHP Validate URL
Feb 15, 2023 programming

PHP Validate URL

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the URLs given by the user to make sure the address has a correct format. ...

4 Min Read Read More
What Modern PHP Looks Like in 2025
Jul 18, 2025 programming

What Modern PHP Looks Like in 2025

PHP has quietly evolved over the years, shedding many of its dated stereotypes while embracing modern programming practices and tooling. What used to be a language mocked for its inconsistencies and spaghetti-code reputation is now a mature, robust, and highly adaptable part of the web development ecosystem. ...

20 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...