Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We started off this chapter by talking about variables. So, it's fitting that we're going to end it by talking about constants. As you can guess from the name, a constant is the opposite of a varible. A vaible can change or vary. A constant can't change. It remains constantly set at the same value. Constants are going to be recognizable on PHP because they're always written in all capital letters and there's no dollar sign in front of them. They're also going to differ from variables in another way. The only way to set a value for constant is to use a function. The define function. You can't just use the equal sign to assign a value. Like you can with variable.
Lets try some experiments. want to open up basic.html. And then we'll just do save as. And we're going to call this constants.php. Change the title here to constants. And let's give ourselves a PHP block. Now, we know how variables work. Let's say we had max width equals to 980. That would be a variable set at the integer 980. But if was a constant max width would not have the dollar sign in front of it and it would be in all capital letters. So it would be MAX_WIDTH, and if we wanted to set MAX_WIDTH to 980 we can't to it by just simply saying 980.
That doesn't work with Constant. Because we're going to be doing them only rarely we need to use this special function to do it. We're going to use define, and then we're going to provide the name of the constant. In quotes, max width. The second argument then will be the value that we want to set it to, and that's going to define max width for us, now notice that we used the quotes here, but most of the time when we talk about the constant we don't use the quotes. Why don't we use them here? Well if you think about it... This is a constant.
But how can we use a constant if it hasn't been defined yet, right? So we have to use quotes until it's been defined. We're just providing PHP with the string that we'd like to use for the name of it, so don't let that trip you up. Now, once it's been defined, then of course We can echo back the value and see that it's been set. Let's just do that real quick. I'll save that and we'll switch back over to Firefox. And instead of typecasting, we're looking for constants. So there it is, 980. That's the value that's being echoed back as max width.
Now, we can't change the value. Let's just see that real quick. I'll put a br tag. And then let's open some more PHP. Let me make a note here. Can't change the value. Max width plus equals one. And then let's echo back max width. See what we get. Alright. Switch back over to our browser. Reload it. Oop. Sorry. Error. You can't do that. It's a syntax error. We can't do adding to this.
Right. Max width equals max width plus one. That doesn't work either. Let's just switch back and try it. Nope. Sorry. Can't do it. Let's take both of those out. Let me show you that you can't even redefine it. Alright, so let's just drop down here, we defined it here. Let's just change this, and let's say, can't even redefine it. So let's say instead of 980, let's make it now 981.
Let's try and echo that back and see what we get. Says oops, sorry. Constant max width was already defined, and then what value did it give me back? 980. It didn't change anything. It basically just objected to this completely, said sorry can't do it, and so max width stayed unchanged. It stayed constant. Now constant stays defined for the duration of the PHP script. So of course once the script ends and all the values are unset, then it can be redefined on the next run of the script. So think back to that diagram that we had of the request response cycle.
Right? When it first comes in and grabs our script and executes it, it defines the constant, the constant stays defined until it returns HTML To the user, but at that point the constant goes away. Everything in our PHP goes away at that point, until the next request response cycle. So I don't want you to think this value is forever set in stone. However, during on execution of your PHP script, that value is going to be defined and it is going to be fixed. Typically, you're only going to use them for things that don't change, things that you really want to be hard and fast. Maybe you're going to define the root path of the place where you're going to store images, right? So you want to have that as a fixed location on this hard drive. This is where you can find your image storage.
That's the kind of thing that you would use a constant for because you're not going to be varying it. You want PHP to have a fixed location for those. So most of the time, you're probably going to want to use variables. And you're going to use constants sparingly. But they're going to be very useful for saving data that's fixed.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104706 Viewers
56 Video lessons · 116608 Viewers
71 Video lessons · 85792 Viewers
131 Video lessons · 41050 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.