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 (2)

0 answers

Post by Steven Morrison on December 23, 2013

Good course, I enjoyed it! :)

0 answers

Post by David Perry on September 7, 2012

Excellent course. Both the Advanced and this one excellently done. Thank you!

David Perry
Perry Computer Services (Ontario, Canada)
Owner
1-877-737-7927
www.perrycs.com

Miscellaneous Topics

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:10
    • Lesson Overview
  • register_globals Directive 0:58
    • register_globals
    • Coding Example
  • $_GET vs. $HTTP_GET_VARS 4:07
    • $_GET vs. $HTTP_GET_VARS
    • register_long_arrays Directive Coding Example
  • Magic Constants 7:30
    • Magic Constants
    • __LINE__, __FILE__, __FIR__, and __FUNCTION__
    • Coding Example
  • exist() & die() 13:19
    • exist() & die()
    • Coding Example
  • Execution Operator 16:23
    • Execution Operator
    • Coding Example
  • Array Operators 18:23
    • Equality (==) and Inequality (!=, <>)
    • Identity (===) and Non-Identity (!==)
    • Union (+) Operator
    • Array Operators Coding Example
  • Variable Variables 24:13
    • Variable Variables
    • Coding Example
  • Variable Functions 28:02
    • Variable Functions
    • Coding Example

Transcription: Miscellaneous Topics

Hello again, and welcome back to Educator.com.0000

Today's lesson is going to be the last lesson in our Introduction to PHP course.0003

We are going to be covering a variety of miscellaneous topics.0007

We are going to be talking about what is known as the register_globals directive, 0013

which is something that you might hear about in talking with other PHP developers and seeing other PHP scripts.0017

We are also going to talk about the difference between the _GET array and the HTTP_GET_VARS array, which we will talk more about in a second.0024

We are going to learn about what magic constants are and discuss the exit and die constructs in PHP.0033

We are going to go over a few new operators that aren't used very often--the execution operator and some array operators.0042

And then, we are going to talk about two sort of advanced topics: what are known as variable variables and variable functions.0049

So, the first thing that we are going to talk about is the register_globals directive.0059

And what it is: it's a directive in php.ini that, when set to on, is automatically going to create global variables for various data0064

that is provided to PHP scripts, like GET inputs.0075

For example, when we had the GET variable that was called name, the way we have accessed it in our scripts is using the _GET array,0079

specifying name as the key to that array.0087

However, if we have the register_globals directive set to on, PHP automatically creates a variable with global scope called name0091

that contains the value of the GET variable name.0100

And so, we could use that variable, name, instead, to access the value supplied along with the name GET variable.0104

This use has been deprecated in PHP as of version 5.3, and this has been done for security reasons.0111

But you may still see code out there that uses that, that doesn't use the _GET array to access GET data.0119

So, I just want to show you what that looks like.0124

If we look at this file, registerGlobals.php, we can see that this is a script that accepts one GET variable called name.0128

And we try to echo that variable using the _GET superglobal array.0138

And then, we also try to do it just using the variable name.0145

And if register_globals is turned on, then we can use this variable name to access that GET variable.0149

The default for the XAMPP installation is to have register_globals turn off.0156

So, if we go ahead and look at register_globals.php, the first thing we are going to see is: we are going to get an error0160

that is going to say "an undefined index and an undefined variable name."0170

The reason it says "undefined index" is because we haven't provided the GET variable called name.0174

So, if we provide name and set it equal to Matthew, we are going to see that _GET ['name']=Matthew.0178

But then, it is going to say that there is an undefined variable name, and it is not going to have a value to output.0185

And if we go and look back at the script, we can see that this is where we tried to output the value of the variable name,0190

that, if register_globals was on, would output the value of the GET variable name.0196

Let's go ahead to php.ini, and I am going to turn on the register_globals directive, which is, again, 0202

a deprecated functionality that is turned off by default now, for security reasons.0209

If we go ahead and restart the Apache web server, and we reload the page, we can see now that PHP knows what the variable name is.0214

It has been defined in the global scope, because register_globals was turned on, and we are able to access the value of the GET variable, Matthew.0224

And just to show you that it actually does that, if I change the name to Joe and submit the form,0231

we can see that both _GET with the name key outputs the value Joe, and the variable name has the value Joe.0237

Sort of a similar thing that is related--it is a deprecated feature of PHP--is that, as of version 4.1, the _GET superglobal0249

that we have been using to access GET data was introduced.0259

Prior to that point, PHP programmers typically accessed GET data using a global predefined variable0263

that was an associative array; and it was called HTTP_GET_VARS.0271

And you would access GET data the same way that you would with the GET variable.0277

For example, you would provide, as a key to the array, the name of the GET variable you were interested in.0280

As of version 5.3.0, however, this HTTP_GET_VARS global predefined variable has been deprecated for performance reasons.0287

But you will still see some code out there that uses it, and you can actually enable this HTTP_GET_VARS array,0299

using a configuration directive in php.ini called register_long_arrays.0306

And what that does is determines whether this long array is available.0311

One thing to note is that, even if that directive is turned on, HTTP_GET_VARS is not a superglobal variable, even though it is a global predefined variable.0316

And it is actually different from the GET superglobal; so the data that is contained in that is not the same as the data contained in the _GET superglobal.0327

But I do want to mention this, because you may see this in some older legacy code, 0336

where people previously used the HTTP_GET_VARS array to access GET data.0339

So, if we go and take a look at a file called registerLongArrays.php, again, it is a script that takes in one GET variable called name.0345

And here, you can see, we try to output that GET variable using the _GET superglobal, which we have been using in the course.0353

And then, we try to do the same thing using this long array called HTTP_GET_VARS, which we just discussed.0360

Now, if register_long_arrays directive is turned to off, which it is by default now, this second line is not going to work, and the first line should.0368

So, if you go and view the script, the first time we run it, we are going to get two errors, 0376

because the _GET superglobal can't access the variable name, because we haven't provided it.0384

So, if we provide a query string that says name equals Matthew, and we hit Enter, we will see that access to the variable using the _GET array worked.0392

It output the name Matthew, which was supplied in the script.0402

But PHP is complaining, saying it doesn't know this variable HTTP_GET_VARS.0405

So, if we go to php.ini (let's turn globals back off), and we turn long arrays on, and go ahead and restart our XAMPP and Apache server,0410

when we reload the page, we should be able to access that variable; and as you can see, we have accessed the GET variable0424

both using the _GET array, and also the HTTP_GET_VARS array.0430

And again, just to show you that that works, we change the name to John, for example.0435

We can see that both of these associative arrays are able to access that GET data.0438

And so, this, again, is a topic that is covered for legacy reasons.0446

I also want to talk about magic constants.0452

And PHP provides a ton of predefined constants available for use in your scripts.0454

We have come across some of those constants when we talked about error reporting, such as E_ALL or E_NOTICE, that defined error reporting levels.0459

Of all the predefined constants PHP has, seven of them are known as magic constants.0468

And they are called magic constants because the values of the constants vary, depending on where they are in a script.0475

So, in a way, they are like constants, in that they are accessed in uppercase notation.0482

They are always available, but their values actually do vary.0487

Out of the seven that are available, the ones that are applicable to the material we have covered so far are these four.0493

There is one called __LINE__, and the way magic variables are named is: they start with two underscores, 0499

then the capitalized version of whatever the constant name is, followed by two more underscores.0505

So, __LINE__: if you use that constant in your script, it is going to output the current line number of the script that is currently being executed.0512

If we had the magic constant __FILE__ with the two underscores before and the two underscores after,0523

and we reference that in our script, it is going to provide the absolute path to the script being executed.0527

Likewise, we have the __DIR__ magic constant, which is going to output the absolute directory of the script being executed.0532

And then, there is also the __FUNCTION__ magic constant that outputs the name of a function that is currently being executed.0539

Let's take a look at a file called magicConstants.php.0547

And in the output section, we have an echo statement that says we are outputting the magic constants from the main script.0552

And here, we just have output the __LINE__, __FILE__, __DIR__, and __FUNCTION__ magic constants from within our main script file.0560

Now, what we also have is an include file, because these values change, depending on what file they are in.0569

For example, __FILE__ and __DIR__ and __LINE__ all depend on the file that they are evaluated in.0575

So, an include file that references __LINE__ might have a different line number than if we use __LINE__, for example, right here0585

in the main script, because it is going to be in a different line number in that include file.0592

So, we have a file called magicInclude.php, which, if we look at it, is just a PHP file that outputs all of the four magic constants we learned about.0596

And as we are going to see, it outputs different values, because these values are for the currently-executed file.0604

And for include files, __FILE__ and __DIR__ are going to be different, because they give the file name and directory name 0610

of the include file, as opposed to the script file, magicConstants.php, when we reference them here.0617

Additionally, we are also going to create a function called magic, and we are going to output the values of all these same magic constants, as well.0626

And __DIR__ and __FILE__ should be the same, because we have defined this function within the magicConstants.php file.0636

However, we are going to get a value for __FUNCTION__, because now we are within a function, so it is going to output the name magic.0644

And __LINE__ is going to be different, because it is going to output the line where this constant is referenced, which, in this case, is line 17.0649

If we go and load this file in our web browser, we can see a couple of things.0657

We can see, within the body of the main script, that the first reference to line is equal to 29.0667

The __FILE__ magic constant lists the absolute path to the magicConstants.php file on our local machine.0674

It also lists a directory of that file.0685

And then, when we try to output __FUNCTION__, the magic constant outputs nothing, because it wasn't output within a function.0687

So, if we go back and look at the main script, we can see that on line 29 0695

is where we have used this magic constant, the __LINE__ constant, which is why we get 29.0700

Now, when we go to the include file, you will notice that the absolute file name changed: it changed from magicConstants.php to magicInclude.php.0708

Now, the directories are the same, because both the include file and magicConstants.php are stored in the same directory of our web tree.0718

You also notice that __FUNCTION__ has no value output, because we are not within a function.0727

And the line number is different because, if we look back at magicInclude.php, 0732

we can see that, in the include file, the __LINE__ constant is referenced on line 3; therefore, it outputs line 3.0737

The other thing is that, within the function--when we are in the magic function, and we output __LINE__,0745

we are going to get line 17 output, because that is where this line constant is referenced.0753

We are going to get the same __FILE__ and __DIR__ values as we did in the main script, because this function is defined in the same file.0757

But we are going to get a value now, when we try to output __FUNCTION__, because we are in the magic function.0766

And the value of the magic constant __FUNCTION__ is just the name of the currently executing function.0772

So, magic is going to be output.0780

So, when we go and look at the output, we can see that __LINE__ equals 17; __FILE__ and __DIR__ are the same as for magicConstants.php.0782

But we now get a value for this __FUNCTION__ magic constant: in this case, it outputs the name of the function, which is called magic.0790

Now, we are going to talk about what are known as the exit and die constructs in PHP, which is something you might see in PHP code out there.0800

And what they are is functions that are used to terminate the execution of a script--for example, if an error occurs.0808

The exit construct and die construct do the exact same thing: they halt the execution of this script.0816

And they have two options that you can use: one option is: you can provide a status string within the parentheses of the construct.0822

that will get output to the user before the script is terminated.0830

The other option you can do is: you can provide an integer in between the parentheses, which represents an error code0833

that gets passed on to the PHP Interpreter, so the PHP Interpreter can understand 0839

if the application ended with an error, and if so, what the error was.0844

Let's take a look at a file called exitDie.php.0849

And what it does is takes one GET parameter, the GET variable name.0853

And here, we use our ternary operator that creates a short variable name that gets set to the value provided, if one was provided, and null if not.0861

And then, we have a test that occurs, that uses this is_null function that we had learned about a few lessons ago.0869

that tests if the name is equal to null; then it is going to exit the script 0875

and output a message saying that the GET variable name is required for this script.0880

That is the example we are going to use of these exit and die constructs: 0884

when you have an error condition that occurs that your script can't run if that error condition occurs.0887

In this case, we are just going to say that the condition of not providing the GET variable name is an error condition that we have to halt the script for.0895

We call the exit function if name is equal to null; it outputs a string that the GET variable name is required for this script.0904

And then, it just says that the script is halting.0912

As soon as that message is output, the script halts at that point.0914

Even though we would exit out of this if statement right here, and go ahead and execute this echo statement,0918

that is not going to happen once we call this exit construct, because it halts the script.0924

Now, alternatively, if we do provide a name GET variable to the script called name, then is_null is going to return false.0931

We are not going to call the exit construct, and we are just going to output the name of the GET variable.0939

If we go and look at this file in the browser, exitDie.php, we can see that, if we call the file without providing any GET variable,0944

it says "the GET variable name is required for this script; script is halting."0952

So, that exit construct actually was executed.0956

Now, if we go ahead and provide a GET variable called name...just set it to some value...Matthew in this case...0959

we can see that it outputs the _GET variable with key 'name' is equal to the value 'Matthew'.0965

And the exit construct does not get executed.0971

And so, that is an example of the exit and die construct, which you may see in different code that is out there.0975

Now, I want to talk about an operator that is not used too commonly: it is called the execution or 'backtick' operator.0985

And it is denoted by a pair of backticks.0990

And what happens is: any time you include a string between a pair of backticks, 0993

what it does is treats that string as a shell command, and it goes ahead and executes that.1000

It doesn't output the result of the shell command; for example, this is a Linux command to output a directory listing.1005

What it does is: it returns whatever output that shell command generates from the operation.1013

And you can store it in a variable: in this case, we are going to store it in this variable called dirListing.1021

And then, you can do with it what you want.1026

Additionally, there is also a function called shell_exec that provides the same functionality as this execution operator,1030

where you would call the function with whatever shell command that you want to execute.1036

For example, we could call it with ls -al, and so forth.1042

Let's take a look at this execution operator in action, which is something you may see in code.1048

Here, we have the execution operator that is just executing, in this case, a Windows shell command that is dir /w.1055

It just says to output the current directory listing.1064

Because this doesn't output the value directly to output, it returns the output of that shell command.1069

We are going to store it in the dirListing variable, and then we are going to use the echo command to output it as pre-formatted HTML.1074

If we go ahead and load the executionOp script, we can see that what gets output1081

is this pre-formatted code, which is the result of calling that dir /w shell command.1090

And so, that is an example of the execution operator.1099

Also, there are a couple of array operators out there that we haven't talked about before that are not used too often.1105

There are several--four, in fact--that are binary comparison operators, and they are just like comparison operators 1111

that we have talked about before, except that they work when both operands of the operator are arrays.1117

There are the equality and inequality operators, which evaluate the same as they did before,1124

except that, for arrays, they evaluate to true if both operands, or both arrays, have the same key/value pairs.1130

So, the keys are the same and the values are the same.1137

And then, it returns false if they are not--if you use the inequality operator.1139

And just like the inequality operator for other variables, there are two versions of it; 1143

and the most common version that you are going to see is the exclamation point with the equals sign.1149

There are also identity and non-identity comparison operators for arrays.1154

The identity operator evaluates to true if both of the array operands have the same key/value pairs 1160

(meaning they have the same keys and the same values, and they are in the same order), and all of the values are of the same type.1166

And if that evaluates to false, then the non-identity operator (which is the exclamation point followed by two equal signs) is going to return true.1172

Additionally, there is also a union operator, which is the plus sign.1183

And what that does is: that is used to append any key/value pairs in one array to the key/value pairs in another array.1186

And what it does is takes any key/value pairs in the right operand that aren't in the left operand and appends them to that array.1199

Let's take a look at these array operators.1208

There is a script called arrayOps.php.1212

I have created three different arrays: one is an indexed array with the values 0 and 1.1215

And then, I have also created an indexed array that contains the values 0 and 1, with the keys (that are strings) 0 and 1.1221

This statement here just outputs these arrays.1229

And I have also created a third array that is an associative array, where the keys and values are both 0 and 1,1232

except array 3 differs from array 2 in that the order of the values is switched.1238

What that is going to mean is that, when we perform the identity comparison operation on these two arrays,1242

it is going to return false, because even though the key/value pairs are the same values, their order is different.1248

For example, what we do down here is: we test the equality operator.1255

And we say, "Is array 1 equal to array 2?" and in this case, it is going to return true, because both the keys and the values are the same,1258

because we know, for an indexed array, the key for the first value is 0 and the key for the second value is 1.1273

And in array 2, the key specified has the value 0 for 0, and the key specified for the value 1 has a value of 1.1279

So, this is going to return true; correspondingly, the inequality operator is going to return false.1287

So, if we go and load this script up (arrayOps.php), we can see that, for this set of arrays,1293

array 1 equals array 2 evaluates to true, so that equality operation evaluates to true.1302

Because it is true, then it can't be not equal, so it evaluates to false.1308

The other thing we are going to do is test the identical operator.1312

And what that is going to test is between array 2 and array 3.1318

And it is going to evaluate to false; and that is because, even though these arrays both have the same key/value pairs1321

(they both have a key 0 with the value 0 associated with it, and the key 1 with the value 1 associated with it),1326

however, their order is switched, so the arrays are not considered identical.1333

It is going to return false; and likewise, the non-identical operator is therefore going to return true.1336

If we look back at the code, the other thing I have done is created two associative arrays, one with two elements1344

where the keys are the abbreviations for the days Monday and Tuesday, 1355

and the values are the full names of the days, with just the first letter capitalized.1359

I have created a second array with the same keys as in the first array, except the values are completely capitalized versions of the days.1364

And then, it also adds a third key/value pair for Wednesday that has the wed abbreviation and WEDNESDAY spelled out in all capital letters.1373

Now, when we perform the union operation (which, again, is used with a plus sign) from array 1 and array 2,1383

if we go back and look at the output, we will see what happens.1389

We can see that, when we perform the union of array 1 and array 2, the output that we get is an array that contains the key mon with the value Monday, the key tue with the value Tuesday, and the key wed with the uppercase value WEDNESDAY.1393

What happens is: any of the new or unique key/value pairs in the right operand get appended to the left operand's array.1411

Because Monday and Tuesday (or the keys mon and tue) were already defined in array 1, those aren't considered new key/value pairs.1421

So, the array keeps the values that it already had associated with those keys.1429

However, the key wed with the value WEDNESDAY does not exist in this left operand array, or array 1,1435

so it gets appended to the array; and you can see that it has the key wed with the value WEDNESDAY.1443

And so, that generates the union of those two arrays.1448

Now, I want to talk about a kind of tricky concept called variable variables that is used somewhat infrequently.1455

What it does is allows you to dynamically declare variables within your PHP scripts.1462

The way it does so is using what is known as a double dollar sign syntax.1467

For example, let's say we have a variable called str, and we give it the value firstName.1471

So, we define it as $str='firstName', which should be capitalized.1477

Then, if we use this declaration here, which is $$str, and then set it to some value 'Joe', 1489

what we have done is created a new variable that has the name that is the value of string.1503

So, for example, we now have (if this was in the global scope) a variable also called firstName whose value is equal to Joe.1511

What happens is: in this double dollar sign syntax, when PHP sees that, it gets the value of the first variable, the rightmost variable.1525

It returns that value, and then it is just as if it had replaced it with the value of that variable; so it would be a dollar sign, followed by firstName.1539

Assuming that that variable wasn't defined already in our script (or even if it was), when we use this syntax right here,1549

and we set it equal to Joe, we create a variable called firstName and set the value equal to Joe.1555

That is a way of creating variable variables, or variables that have variable names.1561

If we look at variableVariables.php (which is a script), I have an array that contains two key/value pairs,1570

the first one being first with the value Joe, the second one being last with the value Smith.1579

What this array is: it is called varsToDefine, which means we are going to use these keys of this array 1585

to define variables called first and last, using this variable variable functionality provided in PHP.1592

So, if this is all that is in our script, if we go and try to output the variables first and last,1599

we are going to get an error message saying that these variables don't exist, because they haven't been previously declared.1604

However, if we go ahead and loop over varsToDefine, and we pull out the key as the variable name to define,1609

and the value as the variable value, and then we go ahead and use this double dollar sign syntax,1618

what we are going to do is create a variable called first, and we are going to set it to the value Joe;1625

and we are going to create a variable called last and set it to the value Smith.1630

So now, when we go ahead and execute this section of echo statements that we also had up here,1634

PHP is going to know that there is a variable called first and know that there is a variable called last,1640

because we created them using this double dollar sign syntax.1644

And so, it would be able to output those values.1648

So, when we go and view this script in our web browser, we can see that, when we try to output first and last 1651

at the beginning of the script, we get these undefined variable messages.1660

However, after we have looped over that array and created these variables dynamically, we end up creating a variable called first and a variable called last.1664

And so, when we try to output their values, Joe and Smith, we are able to successfully do that.1673

And so, that is an example of what variable variables are in PHP.1677

There is a similar concept to that that relates to functions, known as variable functions.1685

That allows for functions to be called dynamically by specifying the name of the function as a string.1690

For example, any time you append a pair of parentheses to a variable name in your PHP code,1697

PHP is going to try and execute a function that is named the value of that variable.1704

For example, if we have a variable called funcName, and it is set equal to the string time, 1710

and then we were to execute the code or type the statement funcName and append it with parentheses, 1721

what PHP is going to do is similar to how it works with variable variables.1732

It is going to replace this variable function name with the string that it represents.1736

So, it is going to remove this, and instead it is going to have the value time.1740

And then, it is going to try to execute a function called time.1745

And so, it is a way of dynamically calling a function, based on the string name of the function.1749

If we go and look at a sample file, variableFunctions.php, we have two functions that we have created, called firstName and lastName.1755

They return a string value that is the first name and last name of this person, Joe Smith.1762

And then, what we do is: we look for a GET variable called function, and we set whatever value the GET variable function was set to...1767

we set the variable function name equal to that.1780

And then, we use this concept of variable functions to dynamically call...in this case, you have the firstName function or the lastName function.1783

And the way we do that is: we supply the name of the function (via a GET variable) that we want to call.1791

So, we are going to supply a GET variable called function with either the value firstName or lastName.1797

The value funcName is going to get set to that value.1802

When we execute the statement here, PHP is going to replace this variable funcName with the value we provided via GET.1805

And then, it is going to try to execute a function that has that name.1813

And it knows to do that because of these parentheses that we have included.1819

If we go ahead and try and view this file in our web browser, we are going to see 1822

a thing that says "undefined index on line 28" and "function name must be a string."1828

So, for example, the undefined index comes because we haven't supplied a GET variable to it yet.1835

And here, it is saying "undefined function" because function name doesn't have a value, because there was no GET value provided.1840

So, if we go ahead, for example, and supply the GET variable function, and set it equal to time,1846

what PHP is going to do is call the time function, which we learned about, which returns a UNIX timestamp for the current date and time.1854

And when we run the script, it is going to output the output of that function, which is the current UNIX timestamp.1861

Now, let's say we pass it a function that doesn't exist...just add some letters to this time...qtey.1869

And we try to run it: we get a fatal error, because PHP is going to try to call the function called timeqtey.1875

And that function does not exist.1884

And that is because, any time PHP sees a variable name followed by a pair of parentheses, it tries to call a function whose name is the value of that variable.1889

That ends today's lesson; it has been a pleasure teaching you this Introduction to PHP course.1905