‫So in PHP we have something called constants.
‫And constants are very similar to variables, but they can't be changed.
‫And they're useful for storing values that you know are not going to change, such as the name of your
‫application, your database info, maybe your version number.
‫And I mean like a version number could change, but it's not very often.
‫And it's not part of the logic in your in your, in your program.
‫Like you would have a variable change or be reassigned.
‫All right.
‫So there's a couple ways to define a constant.
‫And the reason why I kind of want to bring this up now is because we just talked about scope.
‫And with constants you have access to those from anywhere.
‫So let's open up our PHP tags.
‫I'm going to show you two different ways to to do it.
‫The kind of traditional way is to use a function called define.
‫So we can say define.
‫And that's going to take in two things.
‫One is going to be the constant name.
‫So for instance let's say app underscore name.
‫Now it's convention to use all uppercase.
‫We also don't use the dollar sign like we do with regular variables.
‫So with define the second argument that we pass in would be the value.
‫So we'll call it my app okay.
‫And then if I wanted to access that I could say echo app name.
‫If I do that, we see my app.
‫If I wanted if I wanted to define, let's say, the app version and set that to 1.0.0.
‫We could do that.
‫And we can echo app underscore version.
‫There we go.
‫All right.
‫So that's the first way to define a constant.
‫Now we can also use the const keyword.
‫And this was traditionally used in just in classes I know we haven't gone over classes yet.
‫We're going to do that in the next video.
‫But now you can actually define them in the global scope.
‫So I'll say const.
‫Let's say we have db name.
‫We'll set that to my db.
‫And maybe we have DB underscore host set that to local host.
‫Okay.
‫And then down here we'll go ahead and echo out DB underscore name and DB underscore host.
‫Reload and there we go.
‫It's all smushed together, but you get the picture.
‫All right.
‫So.
‫And this blue squiggly line, that's just my spell checker.
‫Just ignore that if you see that.
‫So these are global meaning if I have a function let's just create a function called run.
‫And then I'm going to move.
‫Let's see.
‫I'll just get rid of one of these.
‫I'll cut this.
‫Yeah, let's cut that.
‫And let's put this in here.
‫And we'll cut this.
‫We'll put that in here.
‫So.
‫Oops.
‫Mess that up.
‫All right.
‫So now we're in the run function local scope.
‫And we're going to try to echo out these constants.
‫So let's run the run function and reload.
‫And you see that it works.
‫So we didn't have to do like global app name like we would if it were a regular variable.
‫They're just accessible to us.
‫Now you're not going to be using constants that much.
‫They're usually config values.
‫That's really where you're going to see these.
‫You're not going to use them as part of your basic logic in your application.
‫But it's good to know and and you will run into them.
‫So let's see, the last thing I wanted to show you is if we try to change these.
‫So for instance if I say define.
‫And let's say we want to define app name.
‫Maybe we updated the name or something we can say my app to.
‫So if I do that, I'm already getting an error.
‫Here.
‫It says duplicate symbol declaration.
‫If I reload, I'm going to get app name already defined.
‫So I can't do this.
‫I can go up here and change this value in this single define.
‫But I can't do another define just like I can't do another const db underscore name equals whatever
‫test.
‫If I do that again.
‫Same same error reload, same thing, constant db name already defined.
‫So you can't reassign or redefine a constant.
‫Okay, so in the next video I want to look at type declarations.
‫And I know I said before that PHP is a dynamically typed language.
‫We don't have to declare the type of a variable when we create it.
‫But excuse me, we can change this behavior using type declarations, which was introduced in I believe,
‫PHP five.
‫So this is completely optional, but it can be useful in some situations.
‫And I just want to get you familiar with that in the next video.