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

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Web Application Development

  • empty() is a PHP construct used to determine if a variable is ‘empty’.
  • It returns FALSE if the variable passed to it is ‘empty’, where a variable is considered ‘empty’ if it has not been declared yet OR if its value is:
    • the empty string, ‘’
    • the integer 0
    • the float 0.0
    • the string ‘0’
    • FALSE
    • NULL
    • an empty array
  • Additional Resources:

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:13
    • Lesson Overview
  • Version 7.1 Review 0:46
    • Version 7.1 Review
  • empty () Construct 1:38
    • empty () Construct
    • empty () Construct Coding Example
  • Version 8.0 5:32
    • Version 8.0 Overview
  • Version 8.0 Coding Example 7:08
    • Version 8.0 Coding Example
  • Version 8.1 16:13
    • Version 8.1 Overview
  • Version 8.1 Coding Example 19:48
    • Version 8.1 Coding Example
  • Homework Challenge 26:19
    • Homework Challenge

Transcription: Web Application Development

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

In today's lesson, we are going to be continuing developing of our web application,0005

incorporating what we have learned about nested control structures to improve the application.0008

Specifically, we are going to review what was the state of the application as far as the last version, which was version 7.1.0015

We are also going to introduce a new construct, called empty, 0022

which is useful for form validation and making sure that data was provided to a form as needed.0027

And then, we are going to go over two new versions of our web application, versions 8.0 and 8.1,0034

in which you are going to use nested control structures to add some features.0042

First, as we talk about 7.1: in 7.1, we added some simple GET input validation.0047

And what we did was: we just used the isset method to check whether a particular GET variable we were going to use was supplied or not.0055

However, we didn't really go any farther than that, as far as validating that the data that was input was valid,0063

and was what we needed, and was the right type of data.0068

So, that is what we are going to expand on in this version 8.0.0072

For example, in our checkout form, we are going to make sure that at least one item was selected in the Shopping Cart.0078

And when we go to our thankYou page, when we go to complete our order, 0084

we are going to make sure that the user provided all of the necessary shipping and billing information.0089

Before we do that, I do want to talk about the empty construct, though.0094

This is a built-in PHP construct that is used to determine if a variable is "empty."0100

And it is useful, as I mentioned, for processing form data, to make sure 0107

that specific GET variables were provided to a script, and that they actually had a value.0112

What it does is: it takes a variable in as its argument, and it returns a boolean value, either true or false, 0120

based on whether the variable is considered empty or not.0126

So, the empty construct will consider a variable to be empty if 1) it has not been declared (kind of like the isset statement),0129

or if it has been declared, but it has a value of any of these values: if it's equal to the empty string,0142

the integer or float equal to 0, the string 0, false, null, or an empty array.0149

Let's take a quick look at a script here called empty.php that shows the use of the empty construct.0158

Here, we have a ternary operation, and we run the empty construct on the variable tmp.0165

As we just mentioned, empty returns a true or false boolean value, as to whether or not the variable is empty.0173

As you can see, up here we have commented out the declaration of tmp.0180

So, when empty runs on it, it should return false, because the variable hasn't even been declared yet,0186

because we know, with the ternary operator, that if this evaluates to false, the third operand is going to be output.0193

So, what this variable result does is: it is our way of storing a formatted output of the empty variable,0199

because the empty variable returns a boolean value, in that a true converts to a string (to a 1), and false converts to the empty string.0207

If we just output the result of empty($tmp) directly, it wouldn't look that pretty.0216

So, what we are going to do is: based on whether empty returns true or false, 0225

we are going to set the output string result to be either the value true or false.0229

So, if we run this script with this variable tmp not declared (and let's open a new link), when we go to empty.php, we can see empty($tmp) is equal to true.0233

And that is because we haven't declared the variable yet, which means it's empty.0251

Let's go ahead and declare it, but it still doesn't have a value; so in this case, empty should still return true, because it doesn't hold any value.0257

If we go and refresh the page, we should get the same output; and we do.0269

Now, if we go ahead and set empty equal to 200, for example, it is no longer going to be empty, because it has been declared,0274

and it has a value that is not equal to one of those empty values we mentioned.0283

And if we save the script and reload it, we should now get that empty is equal to false.0287

However, let's say we go back and set tmp equal to 0; or actually, let's set it equal to the empty string,0293

which sometimes happens when users submit a form and don't provide any data in a text input field.0303

So, it's the empty string, which we know is one of the empty values considered to be empty by the empty construct.0311

We should get, now, that the value of empty($tmp) is equal to true.0316

So, if we refresh the page, we can see, "OK, tmp is considered empty, because it contains the empty string."0324

So now, we are going to talk about the changes we are going to make to version 8.0.0334

As it mentioned, we don't have that great validation of GET input data validation going on in 7.1.0339

So, we are going to add some validation to make sure that at least one item is in a user's Shopping Cart.0346

I'm going to add that to checkout.php; to thankYou.php, we are also going to add some validation, to ensure that0352

any necessary shipping and billing information was provided--making sure they provided a first name and a last name 0360

and all of the parts of the address, except for the apartment number, because not everybody has an apartment number.0366

And the validation--we are going to make use of the empty construct that we just learned about0370

to make sure that 1) all of the shipping information inputs were provided, and also that they weren't left as empty.0375

because they might be returned as a blank, as an empty string; and isset would return true,0383

but it would be considered empty, because no value was provided.0391

The other thing that we are going to do is: we are going to be introducing two variables, a boolean error flag0395

(and a "flag" often refers to just a boolean variable that flags a certain condition),0401

and then we are also going to define a variable that holds an error message.0406

And we are going to add that to our three scripts, checkout.php, item.php, and thankYou.php.0412

What that is going to do is make our error processing more efficient--for example, when we find an error 0418

because a GET value wasn't provided, or if a GET value was provided, but it wasn't validated as we needed.0422

Let's go take a look at some of the scripts from this web application.0429

For example, if we look at...this is checkout.php from version 7.1, which is our last version...if you can see, at the beginning,0437

it processes the GET variable; it creates a short form of it.0445

And then, what it does is: it goes right into...as long as the GET variable is not null, which means as long as some item value was provided,0448

it is going to go ahead and start calculating the total or the subtotal.0458

Now, that doesn't really provide any validation to items, making sure that valid input was provided.0463

So, we are going to take a step further, because...one reason is: why bother calculating the total of a cart,0468

for example, if the user did not add any items to the cart?--for example, if all the items were selected at quantity 0?0474

What we are going to do is: we are going to nest an if statement in here that is also going to check0482

to make sure that at least one of the quantities is greater than 0.0487

So, if you go look at our new checkout.php version, 8.0, we can see, we have the same processing of the GET variable at the beginning.0491

We can ignore this error section for now; we'll talk about that in a second.0500

We have the same if statement that checks to make sure that cartItems was at least provided.0504

But then, we have nested another if statement that has another test condition that must be met.0509

And that says that at least one of the items in the cart (either 1001, 1002, or 1003) has a quantity greater than 0.0514

And if that is the case, then it goes ahead and executes this section, which calculates the total.0522

If this condition is not met (meaning that all of the values were 0, or the quantity was 0 for all of the items in the cart),0527

we are going to end up using this error code; it is going to output a message that says, "You need to add at least one item to your cart."0535

Now, what we have done up here is: in this data processing section, we have added two variables.0543

We have added an error flag, and we initially set it to false.0548

And what that is going to do is: any time we encounter an error, such as a GET variable was not passed in at all,0552

or it was provided invalid data, we are going to set error to true.0558

And then, we also are defining a variable errorMsg and setting it to empty string,0563

which is going to allow us to create a custom error message that we can use to output.0571

And the reason for creating these variables is because, at different points in these nested if statements,0577

we are going to have the possibility of generating errors.0582

Let's say, for example, one thing that we do when we generate an error is: we want to set the page title equal to error.0586

Now, if we went down here, and we did that every time we encounter an error, we would have to, in this else statement, write pageTitle=error.0593

And then, we would have to also repeat that down here, and do that for each error.0607

And as our pages get more complicated, and there are more error conditions, we might be doing this four or five times throughout a page.0615

That gives us four or five times to have an opportunity to maybe misspell error.0621

And we know that, any time we have code that is being used in multiple places,0625

we want to see if we can reuse it by putting it in one spot.0632

So, instead of having these page titles here, every time an error is generated, we set error equal to true.0635

In this case, it was true, because the cart was empty.0645

And then, in this case, we are also adding a custom error message that says "You must add at least one item to your cart before you can check out."0651

Down here, this else statement corresponds to if cartItems was equal to null, which means it wasn't supplied at all, so it was a GET error.0659

In that case, we just want to output what is our default message, which is "You have reached this page in error."0667

What we do is: we don't need to define a custom error message, like we did up here.0672

We can just set the error value equal to true, and then once we are out of the data processing section, or towards the end of it,0676

we have added a little section of code that is going to allow us to, for example, define this page title all in one spot.0683

It is going to, if error is true (meaning that an error condition happened), define the title for the HTML title tag to error.0690

And then, what it is going to do is: if the error message is equal to the empty string, 0697

meaning that a custom error message wasn't set (because up here, for example, we set a custom error message),0704

then it is going to output a default error message that says "You have reached this page in error."0710

Now, what we have also done in our code is: in our previous version of checkout.php, when we got to the Output HTML section,0715

we just checked to see if our GET value was equal to null.0723

If it wasn't, we output the checkout form, as would be expected.0727

And then, if there was a problem, we would say, "You have reached this page in error."0733

Well, now what we are going to do is: instead of comparing it to whether cartItems is equal to null, we are just going to say, "Did an error occur?"0737

"And if an error did not occur (we are using the NOT operator), then go ahead and output the checkout form, as expected."0744

Then, we have an else statement that says, if an error did occur, to output the error message.0751

Now, as we saw up above, the error message can be either set to the default error message (and is, in one of the examples) or a custom error message.0757

By using this one error message variable to hold either a default message or a custom message, 0765

we can simplify this to one line of code down here to output our error message.0772

We also did this for thankYou.php; for thankYou.php (and this is the new version, 8.0), as you can see,0777

we have added this error flag and this errorMsg variable to the data processing section, just as we had done in our new version of checkout.php.0789

This is the same code that we had in version 7, that just tests to make sure that the GET data, customer data, and order total were provided.0801

But then, we have added a nested if statement that is going to further validate our input.0808

Basically, we have an if statement here that says, "It is only going to enter the if block," which in this case, 0814

just sets the page title equal to thankYou, meaning that there is no error--"it is only going to do that if all of these conditions are met."0822

Using the empty function, if customer data's firstName was not empty (and we say not with this NOT operator),0830

AND if their lastName was not empty...and we do that for city, street, state, and zipCode--0837

the only one we don't include is apartment, because not everybody has an apartment number...0843

we are only going to output the Thank You message if the user entered all of the appropriate information, 0846

which is everything except for the apartment number.0853

If not, in the else statement, we are going to set an error, and it is going to set the custom error message,0855

"You did not provide all of the necessary billing and shipping information."0862

And then, here again, we are making use of this error message and error flag functionality by...0869

this section is reached if neither of these GET variables were provided.0877

And that is just going to output the default error, which is "You have reached this page in error."0883

Here we have the same error processing section we did on checkout.php.0887

It is going to set the page title to error in the HTML title tag.0891

If no custom error message was created, meaning that errorMsg is still equal to the empty string, then we say, "You have reached this page in error."0896

Now again, when we get to our HTML output section, we test the condition "did an error occur?"0905

If no error occurred, then we go ahead and output the Thank You message, as we normally would on a correctly-entered form.0910

If not, we go ahead and output the error message, which would either be 0919

"You have reached this page in error" or "You did not enter all of the appropriate data into the form."0922

And we also had done this for item.php, as well, in that we added the error flag and the errorMsg to process the error.0933

In this case, the only error that occurs in this file is if the currentItemID is not equal to null,0946

because we are not doing any other validation on the itemID, whereas in the checkout, we validated0951

to make sure the cart had at least one item in it, and the thankYou page--we validated that all of the shipping information was provided.0957

But we did update item.php, as well.0965

Let's close some of these down.0969

So now, to version 8.1: what we are going to do is: we are going to be altering checkout.php,0974

so it not only handles the requesting of the shipping and billing information 0981

that happens when a user says Check Out from the viewCart page,0986

but we are also going to have it output the Thank You message0991

So, we are actually going to get rid of the thankYou.php page altogether, and we are going to have checkout.php do that.0993

The way we can do that is: now that we have conditional control structures and have nested control structures,1000

we are going to create a GET variable called action that we can provide to checkout.php.1006

And based on that variable, we can determine what function we are going to perform, and what we are going to output.1011

In our case, if the action variable is set to checkout, we are going to know that the cart was just submitted.1018

We are going to calculate the total in the cart, and then output the shipping information.1026

The other option that we have for the action variable is Complete Order.1031

After the shipping information has been output, and that form is loaded from the viewCart.php page,1038

what is going to happen is: checkout.php is going to submit the shipping and billing information to itself,1046

so the action attribute of its form tag is going to change.1053

Whereas, before, it forwarded that information on to thankYou.php, now it is going to forward it to itself.1058

It is going to append to that form an action variable that says completeOrder, and then it is going to know to say,1063

"OK, now I am processing a Thank You message," and process the data accordingly and output a Thank You message.1070

All we are doing is taking all of that functionality out of the thankYou.php page and putting it in checkout.php.1077

And there are different schools of thought on this: this is a common way of doing things--1084

by providing an action GET variable to a script that lets it complete different actions,1091

based on what you specified as the action you want completed.1098

The other way is to just, instead of having multiple actions be completed on each page, where you conditionally output HTML 1103

based on the action that was specified--you can have one page just forward the information to another, and on to another.1110

So, for example, viewCart forwards to checkout.php, which would forward it on to thankYou.php.1116

In this case, we are having checkout.php forward the shipping information to itself.1122

And as you will see in the advanced course, when we push things out even further,1128

after you submit shipping information, then it is going to want to request credit card information, for example.1132

We are going to have a different action for checkout.php that is going to allow us to process that, as well.1138

And so, some people like doing each one on a separate page; some people like having an action GET variable1143

that allows you to specify the different actions all in one page.1148

And the reason I am showing it and doing this way is 1) it can be useful and 2) it is something that is used out there often.1153

And as you will see when we start introducing our own user-defined functions, these incredibly long scripts that we have because,1160

for example, if we have three different actions that occur on the same script, we have three different sections of HTML to output.1167

So, that means we have three if, elseif statements to go through.1174

You have the content of three pages in one script; that can get kind of lengthy.1177

When we create user-defined functions, it is going to make things a little bit cleaner,1181

and there is not going to be so much code in all of these pages.1186

Let's go and take a look at the change that we made.1189

This is our old checkout.php from the version we just did, version 8.0.1194

At the beginning, it has one GET variable that it processes, cartItems.1202

And then, it does the check to see if it's null and to see if at least one item in the cart had a quantity greater than 0.1209

And then, it outputs the HTML accordingly--either outputs an error message or outputs the shipping form.1219

In our new checkout.php, we have a new GET variable called action that we are going to process up here, using our ternary operator.1224

And what we have done here is: because now, this checkout.php form is going to perform both the check out operation, 1237

and it is going to perform the thank you operation, it is going to be accepting not only the cartItems' GET data when the action is check out, 1244

but when we go to thank you, which is specified by the setting action to completeOrder, 1253

we are going to need to have access to the customer data and the order total, 1259

which is the information provided by that shipping and billing form that we have.1262

So, at the top of this page, we have added two extra short GET variables.1266

Additionally, when we get to our global data processing center, because now we have to do two sets of data processing1275

(we have to process the data if a cart was submitted from viewCart.php and calculate the total,1282

and if the shipping information was entered and now forwarded to checkout.php, we have to be able to process that1289

and make sure that data was valid), what we do is: we create a set of if, elseif statements that look at the action GET variable.1296

And if it's checkout, it is going to perform all of the operations that were already in our checkout.php to begin with.1308

It is going to check that the cartItems is not equal to null; it is going to check that at least one item in the cart had a quantity greater than 0.1312

But we are adding another elseif statement that is going to say, "If action is equal to completeOrder," which means1320

that checkout.php is going to submit its shipping information to itself, then everything that was on that thankYou page,1328

all the data processing that was involved there, is going to happen here.1336

So, it is going to verify that customer data and order total were provided.1339

And earlier in the script, we saw that we have added those short GET variables to our script.1344

And then, it is going to perform the same data validation that thankYou.php had provided in version 8.0,1350

which is to make sure that all of the required fields for a shipping and billing address aren't empty.1355

And then, it is going to set the page title flag to Thank You.1360

You can see that we have used the same error handling mechanism that we had done before, where if an error occurs--1363

in this case, you did not provide all of the necessary shipping information--it is going to set it to true,1370

and it is going to set a custom message.1375

And as you can see, in this code, because we have even more nested statements, there are going to be more error situations that can occur.1377

So, we would have a lot of redundant code if we output pageTitle=error in each error section.1388

So now, we see the value of declaring this error boolean flag, and then the error message,1396

because it allows us to put a simple message error=true when an error has occurred.1401

And that will know to default to the output message.1407

Additionally, when we set it equal to true for all the errors, when it reaches this error processing code, it is going to set the page title equal to error.1409

The other thing is: we are going to add one more depth to the Output HTML section, as well.1420

We have the same check as before if no error occurred; so if no errors occurred, OK, I am going to output HTML, as normal.1425

But which HTML am I going to output? Am I going to output the shipping and billing address form to request the shipping and billing information?1432

Or am I going to output the Thank You message?1440

So, if action equals checkout, then you output the shipping and billing information form.1444

If action equals completeOrder, which means it is completing the order and going to the thankYou page, you are going to output the Thank You message.1451

And down here, you can see that, if neither of these occur, we are going to output an error message,1460

stating that a correct action wasn't provided, or that the Shopping Cart was entered, but it didn't have at least one item in it,1465

or that not all of the necessary shipping information was provided.1477

One thing to note is that, in our earlier version of checkout.php, in our form that contained the table with text fields1483

for our shipping and billing information, we had the form action set to thankYou.php, so it forwarded all of that data to thankYou.php.1499

Well, thankYou.php doesn't exist anymore, so we are going to change that value to itself, to checkout.php.1507

So, if we look at checkout.php, and we look at the HTML Output section for that, we can see that we have changed that to checkout.php.1515

Now, one other change that we have also made is: because the checkout.php form now has an action variable1523

that gets passed to it, when we submit data from the viewCart.php form, 1529

we also have to submit an action variable that says what action I am completing.1536

From the viewCart, it is going to be checkout; so if we go and look at viewCart.php in our version 8.1,1540

here is the form that outputs all of the item information.1551

If we look at the very bottom, by the Submit button, we have added a hidden field.1555

What that is going to do is pass an action to the checkout page, and it is going to say, "Tell the checkout.php to perform the action checkout."1561

And so, that is how the viewCart communicates to checkout.php what to do.1570

For a homework challenge, as usual, I just want to make sure you understand the different things that we have done in this code.1580

It has definitely gotten a lot more complicated, as we have added a lot more control structures, and especially nested control structures.1586

So, you are going to want to take a look at the code and make sure you are understanding how the flow of the code works--1593

essentially, how now we have input validation that nests everything one level deeper than before,1600

because before we were just validating that GET data was provided; now we are validating that the GET data is valid.1609

So, that gives us two nested if statements and the chance for two different errors,1617

one error being "You have reached this page in error," because no GET data was provided, or 2) the GET data you provided was invalid.1622

So, one of the things I want to make sure you understand is this empty construct we have introduced.1630

We will be using that a bunch, because it is used to process form data and make sure that form data was provided as needed.1633

And then, also, just review the concept of sending an action variable to a script, 1641

in order to get that script to conditionally execute sections of the code.1648

As you can see, in our checkout.php today, the action variable determined 1) what sort of data processing occurred 1652

at the top of the script, meaning whether you were processing the data for a checkout, which would be calculating the Shopping Cart total,1660

or whether you were validating that all of the appropriate shipping information was provided.1668

And then, also, we used that to conditionally output the proper HTML.1674

So, if it is a checkout, we are going to output the shipping and billing form.1680

And then, if it is completing the order, we are going to output the thankYou message.1686

That conditionally outputs the HTML.1691

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.1695