1. 14
    Create a constant in PHP to store a fixed value
    4m 40s

Create a constant in PHP to store a fixed value

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Variables are great, but sometimes you need to be able to set a value in PHP, and ensure it never changes. Constants to the rescue!

Instructor: [0:00] We've defined a variable, but we haven't yet changed the value of it. Variables are meant to be changed or updated at any time. For example, we can change this number of posts by creating another num post variable and adding tend to the previous value.

[0:19] When we go ahead and refresh this page, it will output 20, which reflects the updates we made to this variable. This is sort of a silly example, but you will without a doubt have increasingly complex calculations going on somewhere in your code eventually.

[0:35] Variables are meant to be modified or updated, but sometimes you want to make sure a value never changes. In this case, you can assign the value to a constant rather than a variable.

[0:47] There are two types of constants, global constants and local constants, also known as class constants. Global constants are set with the define function and are executed at runtime.

[1:00] For this reason, they cannot be used within classes, which require the use of local constants. You don't know about classes yet, but we'll touch base on them a little bit later. Let's create a new global constant here with the define function. Define accepts two arguments.

[1:17] The first being the name of the constant, which is common convention to use upper snake case. We will pass this in as a string named min num posts. The second parameter is the value we wish to assign to the constant. We will just set this to zero for now.

[1:38] We can output and use this just like a variable. Let's create a new H3 tag and let's use a short Echo tag to output min num posts. Note how we call that without a dollar sign because it's a constant. Just so we know what this is, let's prefix this with minimum and a colon.

[2:02] When we save and refresh, we will see that the value is outputted just like a variable. Now at this point, we can't duplicate this line and assign another value to it. If we do that, we will get a notice from PHP that says the constant is already defined.

[2:21] This is great because we can enforce the integrity of this value at all times. Since we pass the name of this constant as a string, that means constants defined this way can use something like string interpolation to create dynamically named constants.

[2:39] They can even be defined within loops. Since the define function is executed at runtime, that means they can also be used within if statements, which we will get to a little later on.

[2:51] Any value, including advanced data types, can also be assigned using define. This differs from the const keyword which is used to define local constants. The const keyword has received increased functionality over the years and has a simpler and easier to read syntax. We can create local constants using const just like PHP variables.

[3:16] Let's create another constant underneath this min num post. We will use the const keyword. Let's name this max num post. This naming convention uses the same upper snake format just like the define function, but we can just set this equal to a value.

[3:35] Let's set this equal to 100. Just like the define function, we can specify a maximum and output it just like our other constant. Now, when we refresh the page, we can see that it outputs as expected. Note that creating a local constant looks a lot like defining a regular PHP variable.

[3:59] This format was initially created for classes, but since defining a constant like this is a lot simpler and easier to read than using this defined function, it's now the preferred method of creating constants. If one of these class constants is created within a class, it's only accessible and available within that class. We will learn more about classes later.

[4:23] It's worth noting that the const keyword cannot be used within if statements or loops since it's executed at compile time and not runtime. That means there is still some precedence for using the define function instead of const for specific scenarios.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today