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

Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

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

PHP Array Sort

Today we are going to talk about sorting arrays in PHP. we will take a look at some of the most useful and common array sort functions in PHP and compare their functionality. ...

7 Min Read Read More
PHP Send SMTP Emails with PHPMailer
Mar 22, 2023 programming

PHP Send SMTP Emails with PHPMailer

Today we are going to send emails with the help of PHPMailer package. Sending emails is a very useful feature and in PHP it is very easy and powerful. ...

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