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

How to use Git Alias to increase your productivity (video)
Nov 27, 2022 programming

How to use Git Alias to increase your productivity (video)

Today we are going to talk about Git Alias and how to increase your productivity by adding shorter aliases for commands that we use everyday. ...

8 Min Read Read More
Learn how to contribute to open source projects in 7 easy steps (video)
Nov 27, 2022 programming

Learn how to contribute to open source projects in 7 easy steps (video)

we are going to talk about how to contribute to open source projects We will go through it together step by step ...

14 Min Read Read More
PHP session
Mar 21, 2023 programming

PHP session

Today we are going to learn about session in PHP. Sessions are like cookies but instead of storing the key value pairs on the user’s computer, session stores them on the server. ...

8 Min Read Read More
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

38 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 ...