Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (3)

2 answers

Last reply by: Matthew M.
Tue Feb 7, 2012 2:17 AM

Post by Jacob Case on November 19, 2011

This is proving to be a very thorough and exhaustive course. I haven't found other online PHP courses with comparable scope and depth. The way you alternate the practical application of building a web store with lessons about the fundamentals is perfect for me. THANK YOU!


  • A constant is an identifier that can be assigned a data value that cannot change during the execution of a script.
  • Constants can only be assigned scalar (integer, float, string, & boolean) values.
  • Constant identifiers obey the same naming rules as variable identifiers, except that they do not begin with a dollar sign.
  • Constant identifiers cannot be used within double-quoted strings as variable interpolation does not apply to constants.
  • PHP provides two ways of declaring constants:
    • Using the define() function
    • An assignment statement using the const keyword
  • When using the define() method, a constant can be assigned the value of any expression, not just a static value.
  • $_SERVER is a superglobal variable that is automatically defined and made available by PHP. It contains information about the webserver configuration and the current script.
  • $_SERVER[‘DOCUMENT_ROOT’] provides the full path in the local file system to the currently running webserver’s document root.
  • Additional Resources:


Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:09
    • Lesson Overview
  • Constants vs. Variables 0:55
    • Constants vs. Variables
  • Constant Identifiers 2:28
    • Constant Identifiers Definition and Examples
  • Declaring Constants 3:47
    • Two Ways of Declaring Constants
    • Syntaxes
    • Major Difference in the Two Forms
  • Using Constants Example 6:25
    • Using Constants Example
  • Coding Conventions 11:08
    • Constant Names
    • define () Function
    • Meaningful Names
  • $_SERVER Superglobal 12:23
    • $_SERVER
    • $_SERVER Superglobal Example
  • Homework Challenge 17:40
    • Homework Challenge

Transcription: Constants

Hello again, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be talking about what are known as constants.0004

Particularly, in this lecture, we are going to be going over the difference between constants and variables.0011

We are going to talk about how to name a constant--talk about constant identifiers.0017

We are going to talk about the different ways you can declare constants within your PHP code.0023

And then, we are going to go over some coding conventions that we are going to use for this course when it comes to constants.0027

We are also going to talk about the _SERVER superglobal variable, which is a superglobal variable,0033

like the _get variable that we have used to process form data.0040

We are including that description in this lecture, because it's something that is often used when defining constants in web application configuration files.0046

What is a constant? Well, as the name implies, it's a value that doesn't change.0057

In PHP, specifically, it's an identifier that you can assign a value to.0062

However, unlike a variable, once it has been assigned a value, that value can't change during the execution of the script.0067

This is something that is enforced by the PHP Interpreter; so, if you define or declare a constant, and set it equal to one value,0073

and try to do it again later in your script, you are going to get a warning, and then also the redefinition is not going to work.0080

One other difference between constants and variables is that variables can be used to hold any PHP data type--0087

arrays, the scalar data types, special data types like null.0094

However, constants can only be assigned scalar data types, which, as we know, are integer, float, string, and boolean.0098

It can only be one of those four data types.0105

Additionally, as mentioned, constants are often used in what you would call config files0111

for defining constant values that you use in many places throughout your web application.0118

I say "things that never change," because basically...they change, but they are not going to change during the execution of your web application.0125

Examples might be the sales tax rate, which isn't going to change very often.0134

And maybe you will have an email form on your website, and you always want it to email to a default administrator's email address.0138

That is something you could define as a constant.0146

Now, the way constant identifiers work is similar to variable identifiers.0150

The one big difference is that they don't begin with a dollar sign; other than that, all of the same rules apply.0155

They have to start with an underscore, letter, and then after that, they can be any combination of letters, underscores, and numbers.0162

Here are a couple of examples of four different constants that we have declared.0168

For example, we have declared a different constant for each of the different scalar data types, 0174

which are the only types, again, that you can use to assign to a constant identifier.0179

We created one, DAYS_IN_A_WEEK=7, set the sales tax rate to 10%...we have a string, SERVER_NAME, that we set to

And then, we might have a boolean constant in our configuration file that says, "Are we going to require a password?"0195

And in this case, we set it to true.0202

One thing to note, in particular, is that, unlike variables and variable identifiers, you can't use a constant within a double-quoted string.0206

So, variables--you can make use of variable interpolation; but that does not work for constants.0215

If you output a constant name within a string, it is just simply going to output the name of the constant.0220

We know what constants are and the rules for their identifiers in PHP.0229

Now, let's talk about how we actually declare constants within our code.0234

PHP provides two different methods; the first method is through the use of a function called the define function,0239

and the second is using an assignment statement with the const keyword.0246

And this bullet right here shows the two different syntaxes in action.0252

Here, we are using the define function to define a constant called PI, and we are setting it equal to the float value 3.14.0257

Now, if we wanted to do the same thing, using a constant assignment method, we have a variable name called the assignment operator,0267

and the value we are assigning to it, and then we precede all this with the const keyword.0277

That says that we are declaring a constant pi and setting it equal to 3.14; this value cannot be changed.0282

One major difference between the two forms is that, with the define function, a constant can be defined as the output of an expression.0290

For example, we could define a constant HOME, and let's say we know that the protocol is always going to be HTPP,0299

but we are going to use a variable to set the server name of this home page, and that could be a variable.0309

So, using the define function, we can use an expression in the value portion of the function.0317

And I should mention, actually, that the first argument to the define function is the name of the constant that you want, enclosed in quotes.0323

And then, the value that you want to assign to the constant is the second argument.0333

In this case, we are assigning it a value that is a string, that is the concatenation of this string literal http:/ and the value of the variable serverName.0338

Now, you can't do this with the const assignment version; if we tried to do it down here in the same way, 0353

it would generate a parse error, because using the const assignment method, you can only assign static values.0360

So, we would have to hard-code it as http...0367

That makes the define function, in some ways, a lot more useful; and it is mostly what we are going to be using in this course.0379

Let's take a look at a file called constants.php.0387

Well, before we do that, let's take a look at the actual code that shows you how this defining constants works in real PHP code.0392

So here, we have a define statement that defines, as before, the constant PI, giving it the name PI in capital letters.0402

We are assigning it the value 3.14; and then here, we have an echo statement that echoes the pi value.0410

And remember, as mentioned, you can't include constants within double-quoted strings.0416

So, in order to output it as part of another string, we use the concatenation operator.0420

And then, here we have a constant declaration that uses the const assignment method.0426

So, we are declaring the variable pi, and in this case, we are making it a little more precise, 0436

and we're setting it to the value 3.14159, as opposed to just 3.14, and then echoing it.0441

However, as we know, constants cannot be redefined; so when you try to run this page right here, 0449

when you try to make this second declaration, you are going to get an error generated.0456

Additionally, the constant PI is not going to get updated; so when we echo PI down here, 0461

it is not going to echo the second value that we tried to set it to, but the first one.0466

So, if we go and look at this page, constants.php, we can see, it output 3.14 twice, because PI wasn't reassigned.0471

We also get a notice stating that we had already defined PI within the file.0480

Let me go ahead thing that we could do, if we wanted to make this legal and output a different value for PI--0488

we could create a constant with a different name, called PI2, which would eliminate the error with redefining a constant.0499

And then, we should be able to echo the second value; and let me go through and do this...0506

We don't get the error, and we see that we have the output value of PI2.0511

But that kind of defeats the purpose of what we are doing; I was just doing that to show you how you could change that,0516

so that the error doesn't occur; but typically, you are not going to be doing this.0522

Down here in this other section that I had commented out, I'm going to talk about how you can use expressions within the define function, as we mentioned.0527

For example, we're defining a variable called server, and we're setting it to the value

And then, I'm going to define a constant called HOME_HTTP, which is going to be an HTTP link to that server.0543

And as the value of HOME_HTTP, I'm going to assign it the value of this expression, 0552

which is the concatenation of the http:// string literal and the value of the server variable.0558

And then, in this next line, I am just going to output that variable.0567

Now, if we try to do that same thing down here in a const assignment statement, we are going to get an error,0571

because, as we had mentioned on the slide, constant assignment statements using this method can only have static values.0579

They can't be expressions, which in this case, would be the concatenation of the server variable with FTP.0590

So, for example, if we wanted to create a different constant, HOME_FTP, we couldn't use this type of definition; we would have to replace it.0597

So, if we go ahead and view this page again now, with the section uncommented, we can see that we get a parse error,0606

unexpected . (which is a concatenation operator) on line 43.0614

We go back and look at line 43; we see that this section of code, we thought, would cause an error,0619

because this is not legal syntax; so the way we could replace that would be to just hard-code the link in.0624

And again, this is something you probably wouldn't do; you would usually use the define function, because0635

it gives you this additional flexibility; but to demonstrate this, and to show both of these constants' output,0642

we can make that change, and we won't get the error, and it should output all the information.0649

Here are the two PI values from before, and then we can see the values of the two different constants, HOME_HTTP and HOME_FTP.0654

So now, I just want to talk about some coding conventions that we are going to use in this course for constants.0669

As you have probably noticed, all of our constants were declared using uppercase letters, and spaces between words were replaced with underscores.0674

This is the convention we are going to be using, and it is pretty much the convention that all PHP developers use.0685

It signifies that, any time you see an identifier that is in all capital letters, you know that it is a constant.0691

So, it is what we are going to use in this course, and it is what is used out there in the real world.0697

Additionally, there are those two methods we mentioned for defining constants.0703

We are pretty much going to use only the define function for defining our constants, as opposed to the const assignment statement.0707

The reason for that is: it gives us the added flexibility of using expressions to set the value of the constant.0714

As before, as we talked about in the coding conventions for variables, your constant identifiers should be given meaningful names,0722

so that they describe what their purpose is.0731

Instead of calling something just LINK, you might call it HOME_PAGE, which tells you that the link is to your home page.0734

So now, I want to talk about the _SERVER superglobal.0745

And basically, to mention it again--we haven't talked too much about superglobals, and we're going to get to that later in the course.0749

But a superglobal is a variable that is pre-defined by PHP, and it is made available to you everywhere in your script, and it is automatically populated by PHP.0755

The one superglobal we have used before is the _get superglobal, which is used to access, as we know, GET data that is input to a form.0766

The _SERVER superglobal provides information about the current web server configuration,0778

and also provides a lot of information, like the web server's document root and any HTTP header information for the currently-running script.0784

And it's an associative array, like GET, and one of the more common keys that we are going to use to access this array is DOCUMENT_ROOT,0796

and that provides the full path in the local file system to the web server's document root.0806

And this is useful, because in configuration files, we can use that value to define where include directories are,0812

so that when we include files, we can create an include directory constant, and it will make use of this.0819

And then, that way, when we include a file, it will know which directory to look in.0827

Let's take a look at a file called serverSuperglobal.php.0833

The beginning of this file--what it does is: it outputs the phpinfo.0841

What that is going to do is: that shows all of the information in the _SERVER superglobal, so we can see what it looks like.0850

And then, I have also just added an anchor and a link to the bottom, so that we can quickly get to the _SERVER information,0856

because on phpinfo, it's listed at the bottom.0866

So, if we go and we view this page, and we see phpinfo generated, and click Scroll to Bottom,0868

at the bottom down here, you can see, it has information about the _SERVER variable.0876

It provides all sorts of different information; there is information from HTTP headers about the user agent...all sorts of information.0882

It gives information about the page that was used to refer you to this script page.0891

And in particular, what we are looking for is the $_SERVER ['DOCUMENT_ROOT'].0898

What that says is: that is giving us the document root of the web server that is serving this page.0903

And in this case, it's C:/Users/educator1/xampp/htdocs, which is as we expect.0909

We learned in our lesson on setting up a development environment that the htdocs directory in the XAMPP folder in our User directory0916

is the document root for the default XAMPP configuration.0927

So now, if we go back to this page in the code editor, and uncomment a section we have below,0932

you can see that what I have done is defined a constant called INCLUDE_DIR, which is to represent the include directory.0941

And we set the value equal to the $_SERVER ['DOCUMENT_ROOT'] value, which, as we know, is the htdocs directory.0950

And then, what I have done is concatenated to that a string that directs you to the path of the include directory for, for example, lecture_19.0963

If we had include files in lecture_19, we would put them in an includes directory.0975

So, any time you would want to include a file--for example, 0980

we are in this lecture_19 web application; we want to include a file called header.html--0989

we can use this constant in the file header.html, and PHP will be able to use the constant that uses the $_SERVER ['DOCUMENT_ROOT']0994

variable to resolve this link, and it will know where to find header.html.1008

Let me erase this.1012

So, now that we have defined this constant, I have gone ahead and echoed it.1017

And if we reload the script, it should show up at the bottom of the page.1022

And if we look down here at the very bottom of the page, we can see, it says INCLUDE_DIR, and then it lists the include directory,1031

which is this part right here, which is the document root, which is from the $_SERVER ['DOCUMENT_ROOT'] value.1037

And then, we have appended to it the path to our current include directory.1045

And we can use that, for example, in a configuration file for our web application, to prepend all include files.1050

For today's homework challenge, I just want to give you a little practice with defining constants.1061

We are going to have you use the define function, which is the method we are going to use most in this course.1065

And basically, I want to get you to have some experience with also using expressions to set the value of the constant.1072

So, I'm going to have you define a constant called ADMIN_EMAIL, using the define function.1079

And set its value to a valid email address, and then output its value.1083

That will give you practice defining a constant, and it will also give you practice echoing out a constant,1089

because you will learn that you can't include it in double quotes, as you can with variables.1096

Then, I am going to have you just delete the constant altogether and create two new constants, one called USER and one called DOMAIN_NAME.1102

I am going to have you define the USER constant to be the value that was to the left of the @ sign in your admin email address,1111

and the DOMAIN_NAME constant to be the value to the right of the @ sign in your domain address.1117

Then, I'm going to have you redefine the ADMIN_EMAIL constant, and I want you to use the USER and DOMAIN_NAME constants1123

we just created together to create the same admin email address that was created here in step 1.1133

That is just going to, again, give you a little practice with learning how to use expressions.1141

In this case, the expression is a concatenation operation when setting the value of a constant,1146

and you should be able to output the admin email in this updated script, and it should be the same value as in step 1.1151

That ends today's lesson; thank you for watching look forward to seeing you next time.1159