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

1 answer

Last reply by: Matthew M.
Wed Apr 11, 2012 4:21 PM

Post by Andrew RAFT on March 25, 2012

Hi Mathew,

I enjoy your video and find them very informative, I would like to know how to output the time function to show the time in real time and show the seconds counting on the web browser.

1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 3:31 AM

Post by AnhDuc Nguyen on December 29, 2011

mktime({$dateArr['hours']}, {$dateArr['minutes']}, {$dateArr['seconds']},"
. " {$dateArr['mon']}, {$dateArr['mday']}, {$dateArr['year']})

Why using curly bracket ({} in here? I am confused when using {} and when not use?

Introduction to Functions

  • A function is a group of PHP statements that can be executed on request by other PHP code using what’s known as a function call .
  • When reaching a function call, PHP halts execution of the current script, executes the function’s section of code, and then returns to executing the original script.
  • A function call is made by typing name of the function followed by a pair of parentheses.
  • Function arguments are expressions that can be 'passed' to a function by placing them in a comma-separated list between the parentheses of a function call.
  • Functions are themselves expressions and thus have a value. The value of a function is called its return value , and this value is 'returned' to the calling script.
  • Functions can be chained together by using the return value of one function as an argument to another.
  • provides documentation, via a function reference , for all of the built-in functions PHP makes available to programmers. The documentation provides function prototypes , which are descriptions of the arguments a particular function accepts along with what type of data value it returns.
  • Functions in PHP can be defined to take optional arguments . In the function reference, optional arguments are denoted in a function’s prototype by placing them within pairs of square brackets.
  • Additonal Resources:

Introduction to Functions

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
  • What are Functions? 0:51
    • Definition of Faction
    • PHP and Function Call
  • Function Calls 2:42
    • Function Calls
    • Function Arguments
  • Return Values 4:56
    • Return Values
  • Function Chaining 6:29
    • Function Chaining
  • Function Reference 8:23
    • & Function Prototypes
    • Function Reference Example
  • Optional Function Arguments 12:28
    • Optional Function Arguments
  • String Functions 14:57
    • strtoupper() and strtolower()
    • implode (), str_replace(), explode(), strpos(), substr(), and strlen()
  • Array Functions 25:48
    • count()
    • in_array() and array_key_exists()
    • sort() and ksort()
    • Example: count() and in_array()
    • Example: array_key_exists()
    • Example: sort() and ksort()
  • Date & Time Functions 33:38
    • date() and time()
    • getdate()
    • mktime()
    • Date & Time Functions
    • Example: date() and time()
    • Example: getdate()
    • Example: mktime()
  • Homework Challenge #1 44:31
    • Homework Challenge #1
  • Homework Challenge #1 (Cont.) 45:28
    • Homework Challenge #1 (Cont.)
  • Homework Challenge #2 46:34
    • Homework Challenge #2
  • Homework Challenge #2 (Cont.) 48:06
    • Homework Challenge #2 (Cont.)
  • Homework Challenge #2 (Cont.) 49:17
    • Homework Challenge #2 (Cont.)
  • Homework Challenge #3 50:08
    • Homework Challenge #3

Transcription: Introduction to Functions

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

In today's lesson, we will be providing an official introduction into using functions.0004

Specifically, we will be going over what functions are; we will be talking about several different facets of using functions,0012

such as function calls, return values, and function chaining.0019

We are also going to explore a reference on, which is a function reference that provides documentation for all the built-in functions to PHP.0024

And then, part of that is: we are going to go over optional function arguments, which we will learn about as we go through.0034

And then, we are going to cover some common functions in three different groups: string functions, array functions, and then date and time functions.0040

I said the title of this lesson is an official introduction to functions; and the reason I said that is because we have been using functions already.0052

We were first introduced with the phpinfo function that outputs all of the information about a PHP configuration.0061

We have also used other functions, like the var_dump function that outputs the type and values information of a variable.0067

So, we actually have used them before; now, we are just going to officially talk about what a function is.0075

Basically, a function is a group of statements that can be executed on demand.0080

It is essentially a section of code that usually performs a specific task--for example, getting the length of a string.0088

And this is a task you might perform over and over again, so you set it aside in a block of code, and then you name it.0095

And then, what you can do is: you make what is known as a function call, 0102

which is a way to invoke that section of code and perform the function that you need.0107

What happens is: when PHP is processing a script, and it reaches a function call, it stops execution of the current script, 0114

goes to where the function is defined, runs the function code, and then returns back to the original PHP script after performing the function.0123

Now, as mentioned, PHP has a built-in library of functions that provide commonly-needed tasks, 0133

such as working with strings, working with arrays, and working with dates and times--which are all things we are going to learn about in this lesson.0141

There are also many extensions that are built into PHP, such as a MySQL extension for using the database...and so, those are built in, as well.0148

How do function calls look? How do you make a function call?0164

Well, we have used them before with phpinfo and var_dump; essentially, a function call, 0168

which requests the execution of the code defined in the function, is simply the name of the function, and then a pair of opening and closing parentheses.0173

In this case, the time function is a built-in PHP function, and it gets a current timestamp of the date and time right at the moment.0185

That is an example of a function with what is called no arguments.0194

There are also a lot of functions...most functions accept arguments.0198

What arguments are is expressions provided in between the parentheses of a function.0203

It's a way to pass data and information into the function, so that it can work on it.0210

For example, here we have a built-in PHP function called strlen; what this function does is returns the length of a string that you pass in as a function argument.0216

In this case, our function argument is the string abc, and so what happens is: when this line of code right here is run,0227

when PHP gets to the strlen function call, it stops execution, goes out to the strlen function code,0239

uses this value right here that was input as a function argument to the function, and returns the length of that string.0247

That is the particular function...the string length function.0254

In this case, it would return the value of 3.0257

One thing to note is that functions expect their argument values to be of a certain type; in this case, for example, 0260

the string length--obviously, you pass in a string.0267

And one thing to note is that, if you pass a function arguments that aren't of the correct type,0270

the return value (which we will learn about in a minute) of a function is undefined, which is just stating that the way the function will work is unknown.0278

Another thing to mention is that sometimes I've heard function arguments referred to as parameters.0289

We just mentioned the term return values; what that means is: functions, like variables and literals, are expressions.0298

And because we know from our lesson on expressions, function is something that has a value; a function has a value.0308

What a function's value is known as, is its return value--this phrase right here.0315

What it is: when the function executes, it will return a value to the original script where it was called from.0322

For example, if we have this line of code right here--we have a message variable, and we have a string literal,0329

and we are concatenating to it using the concatenation operator we had learned about, the output,0337

or the value of the strlen function with the argument abc...0345

Now, we just learned that strlen is a function that outputs the length of a string that is passed into it as an argument.0351

So, what is going to happen is: when PHP reaches this section of code right here, it is going to go and evaluate the strlen function.0357

strlen is going to give a return value of 3, so that is the value of the function, and it is going to essentially replace it in line.0364

So, instead of having this anymore, you have 3, because 3 is the value of the function, or the return value of the function.0372

Essentially, this line up here would end up with a message that has a string literal: length is 3.0379

One thing that you can do with functions is known as function chaining.0391

Because functions take expressions as their arguments, and a function, as we just learned, is an expression itself--0394

it has a return value--you can use a function call as an argument to another function.0401

For example, PHP has a built-in function called getdate, and it accepts one argument that is a timestamp 0408

(what is known as a UNIX timestamp, which we will get to in a few slides).0418

So, what it does is: it outputs an array of date information.0422

So, what you can do is: you can use the time function, which creates a timestamp, as the argument to the getdate function.0426

What happens, in this case, when this line gets evaluated here: PHP goes to evaluate this function, and then0437

it sees that its argument is a function itself, so it goes out and it finds the time function.0446

It executes the time function, and whatever value that returns--it then passes that value into getdate.0454

And getdate uses that to produce its output.0460

Functions can be chained together as many levels deep as needed.0464

For example, there is also a function called implode that we are going to learn about.0468

You can, for the function implode...what it takes is an array as its second argument, as we will find out.0474

And because getdate returns an array, you can use getdate as its argument.0482

And additionally, we learned that getdate takes a timestamp, and because the time function returns a value that is a timestamp,0487

you can include that as the argument to getdate, and you can include this whole function chain as an argument to the method implode.0495

One nice thing about PHP is that it has a lot of built-in functionality.0505

And at, there is a documentation known as the Function Reference that provides information 0509

about all of the built-in functions provided by PHP.0516

Specifically, one of the things it provides are function prototypes, which are basically descriptions of how to use the function.0521

It describes the arguments, and the types of arguments, the function takes, as well as what values it returns.0527

Additionally, the documentation just has some general information about what the different functions do.0534

It is part of the PHP manual, and you can find it at this address, which we are going to go to in a second.0540

It documents all of the core, built-in PHP functions, as well as the PHP functions provided by common extensions.0546

For example, it is common to interact with the MySQL database using PHP; and so that is a common extension.0553

So, in this Function Reference at, you can find information about the functions provided by the MySQL extension.0561

Let's go take a look at the Function Reference.0570

The way that you would get there is: if we go to the PHP homepage, and then select Documentation at the top of the page,0576

and English, it brings you to the PHP manual, which we have gone to before.0587

And if you scroll down the PHP manual, there is a section called Function Reference.0591

If we click on that, that brings us to the Function Reference for PHP.0596

What it does (let me blow it up a little bit) is: it lists, by category, all of the different functions that are built into PHP or provided by extensions.0601

For example, if you scroll down, you can see all sorts of different functions that are provided.0613

When you look in the text processing category, there is a subcategory called strings, which is an extension--0619

a part of the PHP core--that provides string functions.0629

And so, when we go to the strings Function Reference, you can see, there is a link called string functions.0633

If we click on that, what it does is: it lists all of the different string functions that are available to you as a PHP programmer.0643

If we go down, and we look at a function we had talked about (let's see, where is it?), the string length function,0654

and we click on that, here we can see what we just talked about as a function prototype.0661

What this says is: it gives the name of the function, strlen in this case, and then it defines the arguments that the function accepts.0668

In this case, it accepts a string argument; and in the sections below, it describes what the arguments are meant to be.0676

And so, this identifier right here is used below; for example, the first parameter is called string.0686

So, when you go down and look at the More Documentation, and you look at the parameters list, you can see string.0692

It tells you that this argument is the string being measured for length.0698

Additionally, what you can tell is that this function to the left of the function name is its return value.0703

And so, this shows that this function, strlen, returns an integer.0710

And if you look further down, there is also a section on return values; in this case, it says, "The length of the string parameter, on success, is returned."0714

"And if not, if the string is an empty string, it returns 0."0723

This provides information--it gives a description of what the function does, and then it provides information about how you would call the function.0726

You would know to call it by passing it a string as an argument.0734

And it also provides information about knowing what sort of result you should get from it--what value it will give you.0737

In this case, we know that it's an integer value.0742

One other thing that you can do in PHP is: functions can have optional arguments.0749

For example, you might have a function that can take one argument, or it can take two arguments.0757

Or maybe, sometimes, it can take even three arguments.0764

Functions can be specified so that they have required arguments, and then also any number of optional arguments.0769

Well, the PHP manual's Function Reference that we just looked at has a way of denoting optional arguments to functions,0775

which is important so that, when you go to look at the manual to review different functions that you may want to use,0784

you will understand that they are optional functions.0789

If we go and look at the date function...and one of the nice things about the PHP website is: at the top of the screen, it has a search box.0791

It says "Search for___ in the___" and it has a dropdown box, and you can select (what is selected by default: function list).0800

If you type the name of the function that you are looking for (in this case, we are looking for the function date), and you hit Enter,0807

it will bring you to the function description page for the date function.0814

And here, again, we have a function prototype; it says the function's name is date; it returns a string.0819

And then, here is where it says it accepts as its first parameter (which is a required parameter) a string (and they're calling it format).0825

And then, the way it signifies its second parameter as optional is by including it in square brackets.0833

So, because this first parameter is not enclosed in square brackets, this is a required parameter.0838

However, because the second parameter or argument is included in square brackets, 0846

that lets you know, as a programmer, that you don't have to provide that.0851

But what it also tells you is that, if you do provide the optional parameter--it tells you that it needs to be of the integer data type.0855

Additionally, for optional arguments, they always have default values.0866

So, here you can see that what happens if you don't provide this optional argument when you call this function:0870

it is going to implicitly assign to this second argument the value of the time function.0878

In this case, it is going to say...if you just call date with one argument, you are going to provide a string to it,0885

and then, for the second parameter, it is automatically just going to use the output of the time function.0890

Now, we are going to talk about some commonly-used functions as they relate to strings.0901

Strings, you work with a lot in PHP; we are constantly outputting and changing around HTML tags.0906

And PHP has a wealth of built-in string functions, and this is a list of some of the common ones.0912

We are going to go through some examples using these functions.0921

These first two--as the name implies, they are used to convert a string that you pass into it as an argument to all uppercase or all lowercase.0925

If we go look at a page created for this lesson called stringFunctions.php, we can see (let's blow this up a little more)0939

a code example of using the strtoupper function.0953

Its use: this is my version of the function prototype; it's string to upper, and it takes one parameter as its value, which is a string.0957

So here, we have an example of calling the strtoupper function, and it would use this syntax here.0968

Here, we are passing it the value Ca, which would be an abbreviation for the state of California.0974

And if we were to output the value of this function, it would output CA.0979

And additionally, here we have the strtolower function, which is similar, except that when you pass it a string value,0988

it converts it all to lowercase letters.0994

Now, one thing to note, if we go and look at the source code behind this, is that in double-quoted strings,0998

we can output variables using variable interpolation.1007

However, even though a function is an expression of value, we can't include a function call within double-quoted strings.1012

So, for example, for this first part--for the strtoupper function example--1020

what we have done is called the strtoupper function on this Ca string, and assigned it to a variable.1026

And then, what we have done is: in our output statement, we have included that variable within our double-quoted string.1034

So, we'll echo it out; and that is because you couldn't replace upper with this--that wouldn't work, and PHP wouldn't know how to evaluate that.1042

However, what you can do, if we look at the strtolower example: instead of having this extra step where we create a variable,1057

and then supply the variable to the echo statement, we can use the concatenation operator (which we had learned about).1063

And so, we put the initial part of our output message; we close that string; and then, we call the strtolower function on the string variable,1070

which here is, for example.1083

This function is going to go out and run and return the value, which is a string in lowercase.1087

It is going to add that string by concatenation to this original output string, and then it is going to concatenate at the end a break statement.1093

That is one thing to be aware of: you can't include functions within double-quoted strings, as you can variables.1104

Some of the other functions that PHP provides is called implode, which converts an array to a delimited string; we are going to look at that in a second.1113

There is a string replace function, and what you can do with that function is replace all the occurrences1126

of a certain string within another string with a replacement string.1132

So, for example, you have a string maybe 50 characters long, and it contains the character a five times.1139

You could use the str_replace function to replace the character a with b.1145

We also have the explode function, which is the converse of the implode function; it takes a delimited string, and it deconstructs it into an array.1150

There is the strpos function, and what that does is: you pass it a string and a character sequence that you are interested in finding1161

in that string, and it returns the position of where that character sequence occurs in the string.1168

There is the substr function, which is going to allow you to return a specified portion of a string,1175

and then the strlen function, which we have talked about, which returns the length of a string.1181

Let's go take a look at some of these functions in action.1186

If we look at our stringFunctions.php file again in our web browser, here we can see the use of the implode.1188

The way implode works is: you pass it a delimiter, and you pass it an array.1198

What it does is takes all the values from the array and creates a string, where they are delimited by the delimiter that you pass into it.1202

So, in this example, we have passed to implode the colon delimiter, and we have passed an array containing three different values.1211

It has two strings and an integer, and what this implode function would output is right here.1218

And as you can see, it's a string of all the values in the array, delimited by colons.1226

Now, one thing that we could do is: let's say we change our mind, and we don't want to use a colon as the delimiter in the string.1233

Well, we can use the str_replace function to replace it with a different delimiter.1239

The way that works is: it has three arguments.1244

The first one is the string that you want to replace, which can be one or multiple characters.1248

It has the replacement string that you want to put in its place, and then the third argument is the string that you will be searching to do the replacement in.1252

In this case, the string we want to replace is the colon character, and we want to replace it with a comma.1262

And the string that we are going to perform this replacement operation on is the delimited string that we had up here,1270

which is the array values, separated by colons.1277

Once we get the output of this replace function, it is then going to give us this right here, which is a comma-delimited list of the three values in the array.1283

Now, we are going to demonstrate the explode function, which is, again, the converse of the implode function,1296

which takes a delimited string and deconstructs it into an array.1304

The way explode works is: you pass it two values--you pass it, as a second parameter, the string that you want to extrapolate into its parts.1312

And then, you provide the delimiter that you want to use to know where to divide the string up.1321

In our case, because we have replaced the colons with commas in our string, we are going to explode, using the comma as our delimiter.1329

We are going to pass it the comma-delimited string.1340

What that is going to do, as you can see, to the output, is: it is going to output an array of length 3.1343

What it is going to do is: it is going to extract everything before the first comma into the first value of the array.1351

It is going to extract everything before the second comma into the second value of the array, and so on.1359

Here, you can say we have an array of length 3, and the three values are 1000, Baseball Glove, and 29.99.1365

Now, we also mentioned this strpos function, and what that will do is: it takes as an input a string, and then,1375

you provide an additional search string that you want to find out what position that string is located at, or if it even occurs in the string.1383

Here, we provided the string position function with this value $1099.99.1392

We are saying we want to find the position of the decimal point, and what we get for output is the answer 5.1402

Now, if we look here, the period, or the decimal point, is actually the sixth character in this string.1409

We have 5 before it, and then 1; but the reason it outputs 5 is because the strpos function uses a 0 index.1419

It names the first position of a string as being 0, so the dollar sign is 0, 1 is 1, 0 is position 2, 1428

the first 9 is position 3, the second 9 is position 4, and then the decimal point is position 5.1436

What we can do is: let's say that, now that we know where the decimal point is, we want to extract out the fractional part of the dollar, or the cents part.1444

Well, we can use the substr function; that takes as its argument a string, and then it takes two integers,1454

one being what position the string you would like to start extracting a substring from is in, and from that position,1461

it uses a third parameter, length, to decide how many characters from that position you gave it you would like to extract.1469

In our case, we pass it the same string, which is $1099.99, and where we want to start is: we want the last two digits in the number.1476

And because we know that the decimal point before is at position 5, and we want to start at position 6,1489

we use as our second parameter 5+1--we would use the position of the decimal point at 5, and we add one to it1499

to say "start at position 6, and then give me a substring of length 2, starting at position 6."1507

And so, our output would be 99, because starting at position 6, the next two characters are 99.1514

And then, just quickly, as we had talked about before, the strlen function outputs the length of a string.1523

In this case, for the decimal portion of the number that we just got, if we had saved it in a variable 1529

and then input it to the strlen function, we would get the output of 2, because the string contains 2 characters.1535

Let's go back to those slides...and now, we are going to talk about some different array functions that are commonly used and are built into PHP.1546

The first one is the analog to strlen, but for arrays; it's count, and what that does is returns the number of items or values within an array.1557

Then, there are two search functions, in_array and array_key_exists.1567

What those functions do is return true or false, depending on whether a value passed to those functions exists in that array.1572

In particular, in_array pass it a value, and it searches all the values in the array to see if the value exists.1580

array_key_exists, on the other hand--whatever you pass it as its value, it searches all of the keys of the array to see if that key exists.1588

Additionally, there are many sort functions, but two ones that we are going to talk about are sort and ksort.1597

What sort does is: it sorts a function based on value; so if all your values were, let's say, integers, in a random order,1606

and you ran the sort function on them, it would resort the array so that the functions 1612

were now in ascending numerical order, which is the default sorting order.1616

One thing to note is that this does remix the array, which can be a problem if you use this function on an associative array.1621

We are going to talk a little bit about that in our example; that is going to be one of the homework challenges--1631

to use the Function Reference at to find a function that will sort an associative array by values, that maintains the indexes.1636

Also, we have the ksort function, which, like sort, sorts the function, but it sorts it based on keys, so it sorts the keys in ascending order.1647

This keeps the key indexes.1658

Let's take a look at a file called arrayFunctions.php, which demonstrates some of these different functions.1662

The count function--it simply takes an array as its one argument.1672

And in this case, we are passing to the count function an array with three integers in it: 10, 8, and 9.1677

And it gives us an output of 3, stating that there are three values or items within the array we passed to it.1684

Now, the in_array function takes, as its second parameter, an array you want to search.1692

And then, as its first parameter, it takes a value you want to search for.1697

So, in this example, we are calling the in_array function; we are saying, 1701

"I want you to look in this array that has the three elements 10, 8, and 9, and tell me if the value 9 is one of those items in the array."1707

And because it is, it, of course, outputs the value true.1716

Now, you can also do this on associative arrays; for associative arrays, we know that this part 1720

to the left of the array assignment operator is the key, and then the part to the right of it is the value.1727

So, when we run in_array on the associative array, it searches all of the values of the array, which are the values to the right of the array assignment operator.1734

So, when we search this array that has three items in it, with each item having the different keys, c, b, and a, and then they have different values,1748

and we are searching if the value Smith exists, this function is going to output true, because, as you can see,1760

the value of the first item in the array, associated with the key c, is Smith.1766

And just to show that this only works on values and not on keys, if we were to run the same function using the same array,1771

but passed it the value c, and said, "Is c in this array?" it is going to return false.1779

So, even though it's a key and it's in the array, it returns false, because in_array only searches the values of an array.1784

Now, on the other side of things, we have the array_key_exists function.1794

What that does is: similarly, it takes an array as input, and then it searches for a particular key (it looks like a type here--this should say key).1800

And then, it returns true or false, whether that key exists.1812

So for example, if we run array_key_exists on a numerically-indexed array that contains the numbers 10, 8, and 9,1816

well, we know from our lesson on arrays that the first item in a numerically-indexed array has index 0; 1823

the second item has index 1; and the third item has index 2; and the index is also known as the key.1830

And so, we are saying, "Does the key 2 exist for this array?" and the output is true, because the value 9 has the index, or key, of 2.1838

So now, we are going to try this on the same associative array before.1849

We are going to ask if it has the string b as one of the keys in the array, 1852

and it is going to return true, because this second key/value pair has b as its key.1858

And again, to demonstrate that this function only searches keys and not values, if we pass the same array,1865

and then ask it to search for the key John, it is going to return no, even though John is in the array,1871

because John, in this array, is a value and not a key.1876

So now, we are going to talk about the sort function; and what that does is: you pass the sort function an array,1882

and it sorts the array by values; it resorts the array in ascending order for values.1889

If we pass it the array with the numbers 10, 8, and 9, in this random order, the output that the sort function would generate,1898

or what it does to the array we pass it, would be: the 0 element would become 8; the second element would become 9; the third element would become 10.1905

You can see, it orders them in ascending order.1915

Now, similarly, if we run this on the associative array, we can see that it sorts by values, and it makes the first value in the sorted array1918

equal to B., because in ascending order, B. is highest up on the list.1932

Additionally, John would come next in alphabetical order, and then Smith.1939

So as we can see, it sorted on the different values in alphabetical order.1944

Now, the thing to notice, that I mentioned on the slide, was that this re-indexes an array.1948

So, in this case, now these a, b, c keys are lost.1953

And as mentioned, one of the homework examples is going to be to research in the Function Reference1957

to find a function that will sort this array right here, but after sorting it by value, it will maintain the key associations.1961

Then, finally, we have the ksort function; and that is a sort by key.1972

And so, it takes one argument, which is an array; so if we pass the ksort function this associative array we have been using all along,1977

it sorts the array by key, so a is higher than b, and b is higher than c.1987

So, the a key is going to be first in the array now; the b key is going to be second; and the c key is going to be third.1993

And we can see that the values that were associated with each key remain with that key.2001

This function is a way to sort by key and keep the key/value associations together.2009

Now, I just want to wrap up by talking about some date and time functions that are available.2020

PHP has a bunch of built-in date and time functions, the first of which that we are going to talk about is the date function, which returns a formatted date.2027

You can optionally pass a timestamp for the current date and time, and then you pass it a format string.2038

And based on this string that you pass to it, it will output or format the string in a particular way.2045

We have the time function, which returns what is known as a UNIX timestamp.2053

We are not going to really get into the details of the UNIX timestamp, but basically, what a UNIX timestamp is, is an integer number2058

that represents the current date and time when the function is run.2065

So, when you run the function time, it returns an integer of time right now, and that integer is known as a UNIX timestamp.2070

Then, we have the getdate function, and what that does is: you pass it a timestamp, and from that timestamp,2081

it can separate into parts the different parts that the timestamp represents.2089

It can separate into hours, minutes, seconds, the day, year, the month, and so forth, and it returns it as an associative array.2093

Also, there is the make, or mktime function, that you can use to build a UNIX timestamp from supplied date values.2102

One thing to note is that date and time functions have a couple of directives within php.ini that they use to configure how they work--2113

in particular, the date.timezone directive; and what you do is: you can go and set that to a particular time zone identifier value,2122

which can be found at this location here, and that will treat all of your date and times as if they were in that time zone.2136

If we go and look at dateFunctions.php, we can see examples of all of these different functions that we just talked about.2146

One thing that you will notice is...before we get into the details of date, I just want to talk about the timezone directive.2160

Basically, what this function does is outputting the current date and time, and it also outputs the time zone.2168

And right now, the time zone output is CEST, which refers to Europe--Berlin, because that is the default value that was set in php.ini in the XAMPP configuration.2174

So, because we are filming on the West Coast for this course, we are going to go ahead and update the php.ini file.2187

And what you can do is: it's quite a long file, but if you search far down in the page, there will be a date section,2196

if I can find it; there it is; and as you can see, it has the date.timezone directive, and it is set by default, in XAMPP, as Europe--Berlin.2212

What we are going to do is: we are going to set it to West Coast time--so we are going to give it the value america/los_angeles.2230

And the reason we use america/los_angeles is because that is one of the allowed time zone identifiers, or one of the identifiers that it recognizes.2240

The way that you can do that is by going to the link that we had in our slide.2250

For example, if we go to this link right here, it will tell you all of the different time zones that are available.2253

We have gone ahead, and we have updated our default time zone.2261

Because it is a change to php.ini, we need to reboot or restart Apache, so that the change will take effect.2264

And if we restart it and go back and look at our date function that output the abbreviated time zone identifier for Europe-Berlin,2275

and we refresh the page, we can see that it changed to PDT, which is the Pacific time zone.2283

Now, where you can find these is on the link that we had before, which can be found by...2294

if you look up the date and time extension in the PHP manual, you can find the list of supported time zones, 2306 would be easier for you to just cut and paste the link, if you had it before.2312

And you can see that it has listed different continents and areas in the world, and you can click on an area, such as America,2316

and see the different values that you are allowed to use.2323

If you look down here, you can see that, for West Coast time (let's see, where is it? My eyes aren't serving me well--oh, it was off the screen)...2326

You can see that they have one for America, Los Angeles, and you can see that it uses an underscore.2352

But for example, if you were on the East Coast, you could use the america/new_york to use East Coast time.2357

So, now that we have set the time zone for this installation of PHP, let's talk about the different functions we had mentioned.2369

Basically, the date function returns a formatted date string.2378

It can take two parameters; and actually, if we go and look at the date function in the PHP manual,2382

and again, we are going to use the search box up here--that is easier, we can see that it takes a string as its first parameter.2390

And optionally, it takes a UNIX timestamp as its second parameter.2397

And the parameter defaults to time, which is the current unit timestamp, if not provided.2402

If we run this function date, which we are here, with just one parameter, it is going to format our date based on the current time.2408

And as you can see, it explains that in the description: "Returns a string formatted according to the given format string,2416

using the given integer timestamp, or the current time, if no timestamp is given," which is what happened in our case.2422

Now, this parameter called format, which is a string--you can see down here, 2430

it provides a lengthy description of all of the different values that you can include in this format string.2434

There are all sorts of different things that you can do: you can use it to output days, months, years, seconds...2440

You can do it with leading zeros or without; and there are just different ways to extract and format different pieces of a timestamp.2447

So, in our example, we have gone ahead and used the formatting string right here.2457

And on your own, you can go ahead and look and research, and see how this format string translates into the output that we get here2464

by looking at this format parameter description; it shows all of the different...basically, they are just like formatting characters...2473

that you can use to output certain parts of the date.2482

In this case, this formatting string formats the current time, because we didn't provide a second parameter.2485

And it says "output the day of the month, the month, the year, a comma, and then output the hour, minutes, seconds, using AM or PM."2491

So, we are not using military time; and then, "also output an abbreviated form of the time zone identifier."2502

Now, as mentioned, the time function, which is our next function we are going to talk about, returns a UNIX timestamp2509

for the current date and time, which...when we run this script right now, and we refresh the page, is going to change.2515

And as mentioned before, a UNIX timestamp is an integer that computers know how to work with,2522

and can decode into the date and time that it represents.2528

That is how the time function works.2534

The getdate function takes as an input a timestamp; and in this case, we could run the getdate function with the time function as its parameter.2536

And so, time is going to generate, as its return value, the current timestamp.2549

And so, getdate knows how to take that timestamp and extract it into different parts, and it outputs an array.2553

And as you can see, it's an associative array that contains all sorts of different keys--seconds, minutes, hours, month, year, and so forth,2561

and extracts the date information and the timestamp.2569

You can see that it knows that, when this getdate function is called, the hour was 4; the minutes were 9; and the seconds were 46 seconds.2572

And then, you can also see that it was June 24, 2011.2586

Now, what you could do is: now that you have these date values, you can use the mktime function to build up a UNIX timestamp.2596

There are different functions for going back and forth between UNIX timestamps.2602

The mktime function works by passing has a specific order of arguments it takes.2606

It first takes hours, then minutes, then seconds, the day and the month and the year, and it will generate a UNIX timestamp based on that.2611

If we go ahead and pass it, the values that we got in this array up here, dateArray, which was received from the current date and time,2620

and we pass it those values--you can see that it generates a UNIX timestamp.2632

And if we notice, because we use the same values that were generated by the getdate function,2636

we should generate the same UNIX timestamp as was used in the getdate function.2644

And as we can see, part of the getdate associative array that it returns--the last value in the array, 2651

with the key of 0, is the UNIX timestamp that it used to dissect all of the different parts of.2658

And as we can see, because we use the same parts down here in mktime, these two values match up.2664

That ends the course material; I just want to talk about a couple different homework challenges that we have for you,2673

that are going to give you some experience working with functions and calling functions in more detail,2679

and using some of the functions that we went over.2683

In this homework example, which you will be able to look over, you can see that we are just going to have you convert string to uppercase and lowercase.2687

And then, practice chaining functions--for example, if you pass an uppercase string to the lowercase function, it would return it lowercase.2694

And then, you pass that value to the uppercase function; it returns it to uppercase.2708

So, by chaining those two functions together, you essentially get the original value.2712

That is one of the things that we are going to have you do.2716

And then, I will have you create an array that just contains five integers, 2717

using float functions to create a string where the integers are separated by colons.2724

And then, like we did in the course, I will have you replace the colon delimiter with commas.2730

And then, we are going to have you use the strpos function in a little bit different way than we used it in the course,2735

to find the position of the first comma in our now-comma-delimited string.2740

And then, we are going to use the substr function to remove the first integer in our comma-delimited string.2745

So, we are going to have to remove the integer, as well as the comma that precedes it.2754

So, just remember, when you do that, that these string position and substring functions consider the first character to be 0 and not 1.2761

That will give you a little experience with that; and it is kind of challenging, and a little tricky, at first, 2770

getting used to using these strpos and substr functions, so it should be a good exercise.2773

Then, I just want to have you run the explode function on our now-shortened list of integers.2779

And then, because explode generates an array, we will have you use the count function to verify that the array length is now 4, instead of 5.2784

For the second challenge, I am going to have you work with some of the array functions we learned about.2796

I am going to have you create an associative array of the 12 months in the year, in ascending order, 2800

so that it starts with January and ends with December, where the key is a string value of the number of the month.2805

And then, the value is the month name spelled out.2814

Make sure that you declare them in ascending order.2819

And then, I want you to look at the Function Reference (and this is to give you practice in learning to use the reference)2821

to find a function that will sort the array in reverse order.2828

Right now, it is sorted from January to December, and I want it to sort the other way around.2834

One way to quickly find the array functions that are available--a little hint I'm going to give you is that, if you type ksort,2840

which we know sorts an array by keys--if you type that in the Function Reference box at the top of the page,2846

if we go and do that quickly...instead of having to search the Function Manual for all of the different array functions,2851

we can see that, because ksort is an array function, that on the left-hand side, it lists all of the other array functions that are available.2861

I want you to go through and find one of these functions that will reverse sort an array.2867

Have it sort by the keys, which are numeric.2877

And then, you verify that by printing out the array to make sure that it's in reverse order.2883

Then, resort the array in ascending order back to its original version, using the ksort function that we had learned about.2888

And verify that that is done with print_r, and then sort the array by value.2896

What that is going to do is: it is going to sort the array according to the value, which is the month name.2902

However, I don't want you to use the sort array; I want you to research in the Function Reference again2912

to find a function that will sort an associative array by value, but keep the key associations,2918

because if we run the sort function on it, what it is going to do is: it is going to sort the months according to value,2924

which would make April the first one, but it's going to give it an index of 0.2930

In order to keep the index (for April, which is the fourth month) of 4, you need to find a new function that will maintain that index.2934

After you find that function and apply it, use print_r to verify the sort by value.2941

And, if it is done correctly, April should be the first month, with a key of 4, and September should be the last month, with a key of 9.2948

Then, I'm just going to have you practice with the array_key_exists and in_array functions 2959

to verify that January is not a key in array, but a value, and that it is a value in array.2964

If you did a search on array_key_exists, it should come up false; if you did a search on in_array using the value January, it should come up true.2972

One thing to note is that these functions return boolean values, and if you try to echo a boolean value,2981

as we learned from our type casting course, the echo statement implicitly casts a boolean value to its string equivalent.2987

And for true, we know that it gets converted to 1, and for false, that gets converted to the empty string.2995

Just keep that in mind when you try to echo the results of these arrays, so that you will understand that it is not going to output true or false.3000

Finally, for our last homework challenge, I am going to have you work with the date and time functions.3010

The first thing I would like you to do is update the date.timezone directive in your php.ini file to your current time zone.3014

And remember to restart Apache after you make the change, because any configuration changes require a restart of the web server.3024

What I would like you to do is use the date function to output the current time in 24-hour format, which would just be the hour and the minutes.3033

And include an abbreviated time zone identifier, so that you can know that the change you made up here3044

to the time zone in the php.ini file works.3049

And what you will need to do to do this is: you will need to research the date function in the Function Reference3054

to learn how to set that first format parameter you pass to date to get it to output the things you want.3062

So, look back at the Function Reference; if you go to the date function, you will have to search this section down here,3067

that talks about the format parameters, to learn about the different format characters you use in order to output a date in the format you would like.3077

Then, I would like you to use the getdate and time functions and chain them together, so that getdate3088

will output an array of the current date and time information.3093

And then, using that getdate array like we did in class, I want you to use the date in that array with the mktime function3097

to generate a UNIX timestamp that matches the timestamp that was used when you called the getdate function...3106

as mentioned, you are going to use the time function.3114

That is going to sort of replicate what we did in the course; and if things work out right, as we have shown,3117

the timestamp that you generate from mktime should match the last element in the getdate array, which has a key value of 0.3123

Those two should match up.3132

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