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

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (1)

0 answers

Post by Nathan Bear on October 3, 2012

How would you go about building a link that can access image subfolders?

Such as mysite/images/teeshirts/1001.jpg

Web Application Development

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:12
    • Lesson Overview
  • Version 11.0 Changelog 0:56
    • Version 11.0 Changelog
  • processGetVar() 1:42
    • processGetVar() Overview
    • processGetVar() Example
  • emailComments() 6:35
    • emailComments() Overview
    • emailComments() Example
  • outputItemLink() 11:19
    • outputItemLink() Overview
    • outputItemLink() Example
  • calcCartTotal() 19:25
    • calcCartTotal() Overview
    • calcCartTotal() Example
  • Homework Challenge 25:56
    • Homework Challenge

Transcription: Web Application Development

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

In today's lesson, we are going to be continuing work on our web application, incorporating what we learned in our last lesson about variable scope.0004

In today's lesson, we are going to go over a new version of the web application, which is going to be version 11.0.0014

And as a part of that, we are going to be adding four new functions to our function libraries.0019

The first one is called processGetVar, which is used to process GET variables.0026

We are going to have an email comment function, in which we are going to extract the part of our contactUs.php script0032

that does the emailing of the comments.0039

We are going to add a function that is going to be used to output item links, and that is going to be used in viewCart.php.0043

And then, we are also going to create a function called calcCartTotal, which is going to be used to calculate our Shopping Cart total.0049

In version 11.0, as mentioned, we are going to be adding these four new functions.0059

What they are going to do is promote us reusing code, as well as aiding in the separation of our PHP and HTML,0063

which, as we began talking about in User-Defined Functions, is an important feature of your web application.0073

These are the files that we are going to be editing, down here.0079

So note, we are going to be editing both outputLIB.php and utilLIB.php, which are function libraries.0082

And then, the other pages that we are going to be editing make use of these two functions that we have created,0090

except for config.php, which--the only reason it is listed here is because we are updating a new version number for that file.0096

The first function that we are going to be creating for this version of the web application is called processGetVar.0104

And it is going to be in utilLIB.php, and it is a utility function that puts common code used on all of our pages that use GET variables0111

into a function, so that we can reuse it in multiple spots.0120

The three pages that we have that use GET variables are checkout.php, contactUs.php, and item.php.0124

And what this function is going to demonstrate from our last lesson is the use of superglobals within local function scope.0131

And as we learned before, there are two scopes in PHP: there is global scope and local function scope.0140

Let's take a look at using this new function.0145

First, if we go, for example, and look at...this is version 10.0 of checkout.php, what we can see here at the beginning is:0152

when we process all of our GET variables, here we have action and cartItems; there is also customerData and orderTotal.0161

And if we look, we can see that basically the same code is being reused over and over.0169

We are using a ternary operator to decide whether to set action equal to a variable specified or null,0174

which depends on whether the GET variable is set or not.0180

And as you see, between this line here (the one for cartItems), the one for customerData, and orderTotal, the only difference in the code0184

is what the variable name is that is included within the square brackets for the GET array.0192

So, what we can do is "parametize" that and create a function that is just going to 0202

accept the name of the GET variable that we want to process, and it is going to perform the same action.0207

So, if we go and look at utilLIB.php, down at the bottom, the function processGetVar is created.0213

And what it does is: you can see here, it's a simple one-line function, and what it does is the same operation that we saw on the last page0224

(I'll move it up a little bit) in that, first of all, it accepts one parameter, variableName, 0233

which is the name of the GET parameter that we want to process.0241

And then, what we do is: we test to see if that variable was set in the GET superglobal.0245

And if it is, we return the value of that GET variable; and if not, we return the value of null.0253

So, we are performing the same operation, but we are doing it within one function.0260

And as you will notice, we are accessing the GET superglobal variable; so this is an example of accessing superglobals within the local function scope.0264

And as you can see here, in the description that I have given of how the function works, along with parameters and return values,0274

I also typically like to include a section that mentions any global dependencies.0281

And so here, I just mention that this function depends on the global variable, and the superglobal variable, the _GET.0285

If we go back and look...this is checkout.php from version 10.0, and if we look at version 11.0, 0295

we can see that all of those ternary operations were replaced with a call to the processGetVar function.0305

And we specified, as the argument to that function, the name of the GET variable which we want to process.0312

This is going to perform the same action; it is going to test if action exists.0319

If it does, it is going to set the short version of our GET variable action to the value that was provided, or input, to the page.0323

If not, it is going to set it equal to null.0334

And as you can see, we have reused the same function down here in all of these other spots.0336

And so, it has actually kind of cleaned up our code a little bit, in that 1) it looks a little neater, in that we have a simple function call,0341

versus a long ternary operator; but also, again, the advantage of doing this and of using it in a function is that we get code reuse.0348

Whereas before, we had 4 different variables that we used a ternary operator to set the value of--that is 4 chances0357

where we could make mistakes in writing that ternary operation--here the only mistake we could make, because we are using the function,0367

is mistyping the name of the function's argument.0376

The other spots...if we look back at the LIB...we could mess up putting the name of the variable here, or the name of the variable here.0381

So, we are kind of reducing that at the same time that we are cleaning up our code.0389

The next function that we are going to create for this version of the web application is called emailComments.0397

What it is going to do is remove from contactUs.php our large section of PHP code that we use 0403

to process the information that was input from the Contact Us form, in order to send out an email.0410

The function is going to be defined in utilLIB.php, and what it is going to do is demonstrate the use of constants.0418

In this case, we are going to be using the email constants, which we had defined previously in config.php.0424

And it is going to show how to use constants within local function scope.0430

Let's take a look at the old version of contactUs.php; this is version 10.0, and we scroll down the page into the final HTML Output section,0435

which we reach, if you remember, when the action provided to this form is equal to contactUs.0451

And you can see here that we create short variable names for the information provided by our contactInfo, which is a GET variable.0459

If we go back and look at the top, you can see that, in this contactInfo's setup here at the beginning of the page, 0468

we set it up as a short variable for the GET variable contactInfo.0479

These are, again, shorter variables that we can use below in creating the email.0487

We can see here where we build up the email message; we create the email header; and here is where we send out the email message.0493

And then, we output a message to the user, saying whether the email sending was successful or not.0502

What we can do is extract all of this PHP code in this section here--extract it out into a function 0508

and replace it with a single function call that is going to perform the same functionality.0516

If we look at contactUs.php version 11, and we scroll down to that same section in the code,0520

now that whole section of code that was related to sending the email has been updated to one line 0528

that is the function call to emailComments.0535

And what it does is passes this GET variable, contactInfo, to the function, so that it can go ahead and build up an email, and then send it out.0538

Now, emailComments is going to return a boolean value, indicating whether the email was successfully sent or not.0547

And we can use that value, just as before, to output whether we are going to output a message 0552

that says "Thank you for your comments" or "There was an error in sending us your comments."0558

One thing to note, too, is that, in version 11.0, at the beginning here, we have also made use 0563

of our processGetVar function for this page, as well, for both the contactInfo and action GET variables.0568

If we go and look at utilLIB.php, and we go to the emailComments function, which is listed here, you can see, the same code0579

that we had in the contactUs page is now moved into this function.0592

We are creating short variables for the contact information data, which is useful in building up the email message.0598

Here is a section where we built up the email message that we are going to send.0605

Here we create the email header, and then, as before, we are going to call the mail function provided by PHP,0608

using our two constants that we defined in config.php.0618

So, this is an example of using constants, which have global scope, within the local function scope.0622

And as you will notice, we haven't had to declare them with the global keyword or use the GLOBALS superglobal array,0629

and that is because that is how constants work, as far as scope is concerned.0635

So, we send out the email, using the mail function, just as before; and what we do is suppress the error,0640

because if an error occurs, it is going to return false; and we simply return the return value of mail,0648

which is going to be true or false, depending on whether the mail was able to be successfully sent to the outgoing mail server.0653

And again, just a reminder from when we talked about the mail function before:0660

just because the mail function in PHP returns true, it doesn't necessarily guarantee that the message was going to be delivered.0663

But it does let you know that it was successfully passed along to the outgoing mail server, and that is as far as that function works.0670

The next function that we are going to create is called outputItemLink.0681

And this is something that is used in the viewCart.php page to output links to each of the different items' pages--their product information pages.0686

And what we are going to use this function for is to take common code that is included in viewCart.php,0697

move it out into a function, and make our code look a little bit cleaner.0702

If we go and look at viewCart (and this is version 11.0), we can see here that we have a link, and this is what is our outputItemLink function.0707

It is going to output this image, the small image of our item, which is a link to the item page.0719

If we click on it, it goes to the item page.0727

We also are going to be creating this text link that is the name of the item, and then when you click on that, it goes to the item page.0730

And as you can see here, we have three different items, and so this is a common function that is being performed.0738

We are outputting a link that contains an image and a name of the item.0743

And here, we do it three different times; so we are going to put that in a function and make use of it.0748

If we go and look at our previous version of viewCart.php, and scroll down the page, we can see here that, as before,0754

we have sections where we list the different items' information.0764

For example, we list item-1001's info, item-1002's info, and so forth.0769

And we have the three different columns of our table: one is the link to the item page, one is the price, and one is the quantity.0773

Well, here is the code that we had before in order to output the item link.0780

We defined the item ID of variable; we loaded the item from the catalog and created some short names to use in outputting our link.0785

And then, here is a section where we output the link to the items page.0794

One is a link that includes an image tag, so the image becomes a link.0803

And then, we also have a span here that includes a text link, that has the name of the item.0808

And the reason we have included them as separate links is just for formatting purposes.0814

But what you can see is: if we look down at, for example, the row for item-1002, you can see that, essentially, this is exactly the same code as before.0820

The only difference is that currentItemID is equal to 1002.0832

So, any time that we have the same code that we are using over and over again, 0837

we want to take advantage of being able to put it in on spot, and therefore only having to change it in one spot.0840

So, we are going to put it into a function.0844

What we are going to do is: we are going to create a function called outputItemLink.php.0847

This is going to be in the outputLIB.php function library.0855

And the reason for that, again, is that we put all of our functions that output HTML into the same function library,0858

and for this web application, it's outputLIB.php.0866

So here, we can see the new function that we have created, outputItemLink.0869

It takes one function parameter, which is the item ID of the item that we want to look up and output the link for.0875

And what you can see here is: basically, it is the same code, slightly modified, that we had within the actual viewCart page.0889

But as mentioned, we used this code three times, so we are going to go ahead and put it in a function,0899

so we can just call the function three times, as opposed to actually having to write this code three times within our file.0903

Not only is it going to increase the maintainability, because we have less chances for mistakes,0908

but also, it is going to, as we will see, make our code look a little bit cleaner by getting rid of PHP code contained within an HTML file.0913

One important thing to notice is that this function makes use of the GLOBALS superglobal array.0922

And it does that in order to load the item catalog, which, if you remember, is contained in our catalog.php file.0928

It is a global variable, and so, in order to access global variables within local function scope (and this is a function),0936

we need to either use the global keyword or access them by the GLOBALS superglobal.0944

And here, that is what we have chosen to do; we could have also done it with the global keyword, as well.0950

And so, this function demonstrates using global variables within local function scope.0955

It also, down here, demonstrates, again, using constants within local function scope; 0960

and here, you can see, we are using the IMAGE_DIR constant when we output our image tag.0965

And again, constants have a global scope, and they don't need to be prefaced with the global keyword when they are being used within a function.0971

So, this is the same code that we had within the file, but we have included it in a function.0978

Now, if we go and look at viewCart 11.0, and we go down and look at each of the rows for all of our different items,0983

here we have, for item-1001, significantly reduced the amount of PHP code within our HTML file.0994

Here, we have just defined currentItemID, as before, which is 1001.1004

And now, for the column where we would output the item link, which is the image and the name link to the item's page,1009

we simply have a function call that we echo, and we pass it the current item ID, which is 1001.1017

And as we know from just looking at the function outputItemLink, we can look up the global variable using the GLOBALS superarray,1021

and be able to load all of that item's information.1030

And then, here we go ahead and output the price, and our text box that receives the quantity of items the user desires.1032

Now, one thing to note is that the price column changed a little bit, because in our previous version, if you look,1041

because we actually loaded the item from the item catalog within viewCart.php,1049

we went ahead and created a short variable called curPrice that represents the current item's price.1055

And so, when we went down here to our column for outputting the item's price, we just referenced that short variable.1062

Well, in our new viewCart.php, we don't load the item here; we load it within this outputItemLink function.1070

So, we have to load that price manually here; and so here, we are, again, looking up (in itemCatalog) the current item,1076

which has the item ID, curItemID; and then we are getting the price value out of that array for that particular item.1088

And then, we are outputting it; and as you can see, for item-1002, we have the same thing.1100

We have replaced this with outputItemLink; for 1003, we have replaced the whole section with outputItemLink.1105

So, we go back up to item-1001; there is the start of item-1001.1110

And if we look at the same one on version 10.0, here is the code and HTML for outputting the link to item-1001.1122

As you can see here, we have a lot of PHP code contained within this first td tag.1133

Now, it is simply replaced with this one function call, so that really neatens up our file, and again, separates our HTML and PHP,1141

which is good, because if we have (for example) developers that work on PHP specifically, and developers 1148

that usually just work on the HTML portion of the site, it allows the HTML developers 1153

to work on the page without having to be involved with the PHP code itself.1159

The last function that we are going to add to this version is called calcCartTotal.1168

And what that is going to do is take the calculation operations that we did at the top of checkout.php,1172

when calculating the Shopping Cart total, and move them out into the function.1181

And again, the reason for this is to remove as much PHP code as we can from our HTML/PHP files that eventually get output to the user.1184

The function is going to be defined in our utility library, called utilLIB.php.1196

And what it is going to do is access global variables; it is going to access the item catalog that is defined globally in catalog.php,1201

which we include in all of our pages.1209

But it is going to access it differently: as opposed to using the GLOBALS superglobal, like we did in outputItemLink to access it,1212

or even using the global keyword, we are going to pass the item catalog itself into this calcCartTotal function.1219

And what that does is: it is going to reduce this function's dependency on the global variables.1234

So now, this function, in a way, becomes more of a standalone function that can be reused in other spots,1242

because the function will always be able to work in a way, because it is always going to have an item catalog passed to it.1246

So, it will always have an item catalog to work with.1251

If we directly access the global item catalog variable from within the function, using GLOBALS superglobal or the global keyword,1254

then we become dependent on the fact that that function is always going to have to work in a PHP web application 1263

where that global variable is defined.1269

So, this is going to improve our function's reusability.1271

Let's go take a look at our old checkout.php page.1277

If we look at the top section, where we calculate the total of our cart, we have a bunch of arithmetic operations here1284

where we multiply each item's price times the quantity the user submitted, and cumulatively add it up.1296

Then, we multiply it times the sales tax rate to get our final total including sales tax, and then round it off.1304

Well, what we can do is remove all of this code right here out into a PHP function that we are creating called calcCartTotal.1311

And we are going to reduce this all to a simple function call.1320

If we look at version 11.0 of checkout.php, whereas we had all of those calculations before, right here in this section,1324

now it has become a single function call.1338

It is a function call to calcCartTotal; it is going to return the total calculated, which we are going to store 1341

in the same variable that we had before, curTotal for current total.1345

calcCartTotal is going to take in two parameters: the first one is cartItems, and cartItems is (if we look at the top) a GET variable.1350

that is passed to the form from viewCart.php, and is an associative array that lists the item ID as the key,1363

and then the quantity selected by the user as the value, of the array.1372

We are going to pass that GET data on to our function.1375

And then, we are also going to pass along itemCatalog; and because we are in global scope right now1381

(we are not within a function in this script), itemCatalog is accessible.1387

It is a global variable, which we can find in config.php, and it has global scope.1391

So, we have access to it, so we are going to pass that variable directly to the function, so that it doesn't need to use1395

the GLOBALS superglobal or the global keyword in order to look up the item's price within the item catalog.1401

If we go and take a look at utilLIB.php, and we look at the function we have created called calcCartTotal,1410

here you can see, basically, it has the same code that we had used before, and we have just put it into a function.1418

And what it does is uses the item catalog that was passed into the function, as opposed to directly accessed using the global keyword,1427

to look up each of the items' prices, and then it multiplies it times the quantity of the item that was selected by the user,1437

which is the value associated with the key that is the item ID of the item you are interested in, in cartItems.1446

Again, cartItems is the GET variable that was passed to checkout.php from viewCart.php.1458

So, we perform the same calculations and cumulatively add up the total.1466

Here, we do have a little bit of global dependence, in that we are accessing the sales rate constant, which we had defined in config.php.1471

So, we go ahead and add the sales tax to the total, and then we return the total, rounded to two decimal places.1482

We are essentially performing the same things as before.1488

And as you can see up here, for global dependencies listed, we have a global dependency on the constant sales tax rate.1491

And actually, if we go ahead and look in another file, outputLIB.php, and look at our outputLink item link function,1499

there are actually two global dependencies; one is on the constant IMAGE_DIR, 1515

and one is on requiring that this item catalog, global variable, be available.1518

So, in this new function that we have created, we don't have that dependency any longer--which, in general, is a good thing.1526

And so, here, once again: what calcTotal does is return the rounded-off value.1539

And then, when we go and look back at checkout.php, it returns that value to curTotal,1546

which we can use throughout the rest of the page, to output the total as needed.1551

For today's homework challenge (it is typical of our web application lessons), I just want to make sure 1559

that you understand working with local function scope and global scope, 1564

and understanding how, within our functions, we can access superglobals, constants, and global variables,1571

and the different ways that we do that.1578

Superglobals are always available; constants are always available without having to use the global keyword.1580

And then, any other global variables defined in our main script can be accessed by the global keyword or the GLOBALS superglobal.1585

Additionally, just note how, for item, outputItemLink, and calcCartTotal, we essentially are accessing global variables in different ways.1594

In this one, we are accessing it directly, using the GLOBALS superglobal array.1605

And here, we are reducing the dependency on the global variable by passing the catalog itself into the function,1610

so it has direct access to it without having to use the GLOBALS superglobal array.1620

The other thing is just to be sure that you understand, for step 3 here, how separating out the PHP code into our 4 different functions1629

is going to promote code reuse, because, as we saw several times, we are taking similar sections of code and "parametizing" it,1636

putting it into one function that we can call over and over.1645

So, if there are any problems with that code, we can edit it in one spot.1648

As we will see as our web application builds up, that is also a good thing, because then, maybe, if we change 1652

how we want to (for example) process a GET variable, we only have to do that in one location,1656

as opposed to going through and doing it in every location where we process a GET variable.1661

And also, understand how doing functions allows for the separation of PHP and HTML,1666

where we are taking large sections of PHP in what are essentially files that are output to the user (they are .php files that contain1673

a mixture of PHP and HTML), taking the PHP out, and moving it into a function file.1681

And it cleans up our output pages that maybe an HTML developer would work with.1688

So, in the end, this ends up making a more flexible and easier-to-maintain web application.1694

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