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 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
PHP Method Chaining
Mar 22, 2023 programming

PHP Method Chaining

Today we are going to talk about chaining methods in PHP. It’s cleaner and more readable and I don’t have to refer to my object every time I want to run one of the methods. ...

7 Min Read Read More
PHP Array Unique
Feb 14, 2023 programming

PHP Array Unique

Today we are going to talk about array unique function in PHP. It's a very good way to remove duplicated values from an array and change it to an array of unique values. ...

5 Min Read Read More
PHP Array Flip
Feb 14, 2023 programming

PHP Array Flip

Today we are going to talk about array flip function in PHP. Array flip is very simple yet useful array function that helps you flip and array. ...

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