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

0 answers

Post by petar vukasinovic on October 10, 2012

Hello Matthew i have a question for you. Im learning from you php and i will go to advanced php but can you tell me one thing. This is my first time to see php, and i have lots of stufs clear in my mind right now but can you tell me how i can learn it better i mean with more understanding.
Im practising what i learn from you every day but should i make a project like web app but alone or something pls tell me if you have idea.
thank you i love your lessons :)

Variable-Length Parameter Lists

Variable-Length Parameter Lists

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

  • Intro 0:00
  • Lesson Overview 0:09
    • Lesson Overview
  • Variable-Length Parameter Lists 0:34
    • Variable-Length Parameter Lists
    • Coding Example
  • Variable-Length Parameter Lists (cont.) 5:21
    • When a Parameter List is Defined For the Functions
    • Coding Example
  • Variable Type Functions 9:54
    • is_int (), is_float (), is_string (), and is_bool()
    • is_ array ()
    • is_null ()
    • Variable Type Functions Coding Example 1
    • is_numeric() and Example
    • Variable Type Functions Coding Example 2
  • Homework Challenge 19:35
    • Homework Challenge
  • Homework Challenge (cont.) 20:52
    • Homework Challenge (cont.)

Transcription: Variable-Length Parameter Lists

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

In today's lesson, we are going to be talking about user-defined functions with variable-length parameter lists.0004

Specifically, we are going to be talking about what variable-length parameter lists are and how you can define functions that have variable-length parameter lists.0012

And then, we are also going to talk about some built-in functions to PHP that relate to finding out the types of different variables.0020

And that is something that is often used with variable-length parameter list functions.0027

User-defined functions in PHP are permitted to accept any number of parameters.0036

The functions we have worked with before have had required parameters and optional parameters.0042

But all of those are explicitly listed in the function's definition.0048

In a variable-length parameter list function, you can supply arguments or parameters to a function 0051

that aren't even defined in the list, whether required or optional.0058

PHP provides a couple of built-in functions for being able to deal with these types of functions.0062

And they are only user-defined functions: there is one called func_num_args, which is a function that returns 0069

the number of arguments that were input to a particular function.0075

func_get_arg, which allows you to get the value of a particular argument that was passed in, and then func_get_args, 0081

which returns an indexed array of all of the function arguments that were passed to a particular user-defined function.0089

Now, one thing to note is that these functions interpret the argument list as being 0-indexed, just like an array--0097

meaning that they treat the first argument as being argument 0, rather than argument 1--so it works just like arrays.0104

If we take a look at a file called variableParams.php, I have created a function here called add.0112

And as you can see, it has no defined parameters; it has no parameter list.0119

But in fact, it can accept multiple parameters.0126

And what this function does is: as many numeric parameters as you provide to it--0129

it is just going to add them all up, however many you provide, and then return the sum.0135

For example, here we call the function add 1, which means we are providing one argument to it, and it's the number 1.0140

What is going to happen is: when we enter the function, we set up a sum variable, which we just set equal to 0.0149

And then, what we do is use a for loop to loop over all of the arguments provided to the function.0155

So, we set i equal to 0, which is our counter; and then, we say, "As long as i is less than the number of arguments, go ahead and evaluate the loop."0161

And the loop evaluates by just cumulatively adding the value of each parameter to this sum variable.0169

And the way it does that is: you can see here, we made use of the built-in function func_get_arg.0177

And what we do is: we pass it the number of the argument that we want to get; 0184

and in this case, for the first time through this loop, it's going to be 0.0191

And that is because, as mentioned, in an argument list, the first argument is considered by these functions to be 0.0194

So, the first time this loop goes through, this is going to say, "Give me the argument at index 0," 0200

which is going to be the first argument, "and add it to the sum."0206

It is going to do that for each of these arguments.0209

Once it reaches the end of the number of arguments that we have, or it has looped over all of the arguments, it is going to exit.0211

In this case, it is only going to loop through one time, because we have only provided one variable to it.0217

So, it is going to be 0; then, when the loop finishes, it is going to increment to 1.0222

And 1 is not going to be less than 1, which is the number of arguments, so it will end.0227

If we run this script, we can see that sum equals 1 when we pass it one parameter with the value 1.0232

Now, let's say we want to add multiple numbers: 1, 2, and 3.0243

Now again, this function doesn't have its specified parameter list; it says that it accepts three functions, 0250

but it has the built-in capability, using these built-in functions from PHP 0255

(the func_num_args and func_get_arg) to be able to add up all of these together.0260

So, the result should be 6; and when we reload our page, we can see that that is the result.0265

And you can do it indefinitely: you can add as many arguments as you want.0270

We add a 4 and a 5; it should sum up to 15--and as we see, that is going to be the case.0275

The other thing to notice is that, if you don't provide it any arguments, that is still going to work.0283

When we enter it, the sum is going to be equal to 0.0290

We are going to set the counter equal to 0, and we are going to say, 0293

"As long as i is less than the number of function arguments, we are going to execute the loop."0295

Well, the number of function arguments is 0; 0 is not less than 0, so this is never going to get executed.0298

So, it is just going to return the sum, which is going to be the default value, its initial value, which is 0.0303

When we run this, we should get sum=0.0309

And so, that is an example where you don't even have to pass parameters to functions that accept variable-length parameter lists.0311

As far as variable-length parameter lists go, you can also supply a variable number of arguments to a function that has a defined parameter list.0324

Like with the functions we have worked with before, where we have the required or optional parameters, 0337

you can also supply additional parameters on top of that--additional function arguments.0342

And then, one thing to note is that, for those arguments that you have already defined parameters for (for example,0351

let's say you call the first one required 1, the second one required 2), you can access those parameter values 0360

(or the values passed in for those parameters) when you call the function by the variable names that you give them.0365

(for example, required 1 or required 2); or you can access them by the built-in functions for dealing with variable-length parameter lists.0372

For example, even though the first parameter might be called required 1 in our function definition,0379

we can also access that with a func_get_arg with the value 0, because that is going to say, "Give me the value of the first argument."0386

Let's take a look at how this works in a script called variableParams2.0394

And here, we have just updated the function add, and we have had it so that it requires two arguments.0400

It requires an op1 and an op2; and what it does is takes those two arguments and adds them together.0406

Then, it provides the additional functionality that you can supply it any other number of arguments,0413

and it is going to add that to the total of op1 and op2.0418

But these first two arguments are required; and if you don't supply them to the function, you are going to get an error from PHP.0421

Now, as you can see here, one thing to note is that, when we loop over all of the "extra" arguments provided to the function,0428

all of those variable-length parameters, we start our loop counter at 2, because if we started it at 0,0435

because we have already added op1 and op2 to each other, if we started it at 0, it would add them again, and our sum would be incorrect.0445

So here, we started it at 2 and said, "I want to add, in addition to op1 and op2, any additional arguments from the third argument on."0452

So, for example, if we pass it the two required parameters 100 and 1, we should get the sum 101.0461

If we load the page, that is what we get.0469

Now, if we were to call it, for example, with just one parameter, because they are listed as a required parameter,0472

we are going to get a warning from PHP.0478

And it is going to say "missing argument2 for add."0481

Let's go back and correct that; and now, let's add some additional arguments: 2, 3, 4, which should give us the sum 110.0486

And so, when we go and reload the page, we should see that our sum equals 110.0498

And so, the thing to note here is that the first two arguments are added up by accessing them 0506

using their defined parameter names, op1 and op2, which were defined in the function definition.0521

But as mentioned on the slide, you can access them using this function func_get_arg, as well.0523

So, we could cancel this out here; and even though these are defined parameters, we don't have to access them using their variable names given.0527

We can set sum equal to 1 and have the content of this loop be the same as it was before--as in, add with no required parameters.0537

Here, we have two required parameters, but it is still going to work with the same code,0546

even though we are not accessing op1 and op2 using those variable names.0550

So, when we run this function again, it is still going to give us 110.0554

And we can see that, when it reloads, that is exactly what happens.0559

And just to make sure, let's remove a couple of these parameters so that we get 103.0561

So, you can see that the function still works; so, for parameters that are defined, you can access them using the func_get_arg function.0573

Or, you can access them directly by calling them by their variable name that you defined in the parameter list.0586

PHP also provides a bunch of built-in functions that are useful for testing whether a variable is of a certain type.0595

So, when you have functions that accept variable-length arguments, sometimes you may have a function 0603

that can accept different kinds of variables and do different things, depending on what is provided.0609

Maybe if it provides a string as a variable argument, it does one thing; maybe if it provides an integer, it does another thing.0613

And these functions can also be used in functions that don't allow variable-length parameter lists; but that is one of the areas where they are used.0619

These first four here: you supply the function with a variable, and it returns true, in this case, if the variable is an integer data type.0629

Here, it returns true if it's a float; if not, it returns false.0640

Likewise, for string and bool, the same thing happens: if it's a string or a bool, it returns true; if it's not, it returns false.0644

Additionally, you can test if something is an array; and that just returns true if it is an array--it doesn't matter whether it's indexed, empty, or associative.0650

An empty array, even--it is going to work: it will return true.0659

And then, we have a function is_null; and we had already kind of been using that.0661

You can use that in a test condition--for example, let's say we had an if statement: you could say is_null ($var).0665

That is the same thing as if we had typed if var=null; it's another way of using that.0677

Let's take a look at a script that uses some of these variable-type functions.0689

What we have here is a function called outputType; it accepts a variable number of arguments, so it has a variable-length parameter list.0695

And the first thing that we do, in contrast to the other implementations we used for variable-length parameter list functions:0704

we are going to make use of this func_get_args function that is built into PHP.0715

And what that does, as mentioned: that provides an indexed array of all the arguments provided to the function.0719

Then, what we are going to do is use a foreach loop to loop over this args array, which is an indexed array of all the arguments.0725

We are going to pull out the key (which is going to be 0 for the first argument, 1 for the second argument), 0733

set the variable argNum to that key, and then set the variable argVal equal to whatever the value of that argument is.0738

For example, if we look down here where we call the function, we are calling it with a number of different literal values.0749

And so, for example (1, 2, 3, 4, 5...), we are providing it 6 different values; so this is going to be a length 6, and this loop is going to work 6 times.0757

Now, what we are going to do within the loop is: we make use of these is_int, is_float...0766

these variable-type functions that are built into PHP to perform a different function.0770

In this case, we are outputting information about the variable, but just in a slightly different way.0774

For example, if argVal is an integer, we are going to say, "Argument number (whatever argument number it is) is an integer,"0780

and then it has the value, and it outputs its value.0787

If it is a float, we are going to say "argument xx is a float"; the same thing for a string; the same thing for a boolean.0790

For the boolean here, we use this ternary operator, because as we know, when you convert a boolean literal to a string,0797

true just comes out as 1, and false evaluates as the empty string.0808

So, what we are going to use is the ternary operator to say, "If argVal is equal to the boolean value true,0812

we are going to output the string 'true'; if it is equal to false, we are going to output the string 'false', as opposed to 1 or the empty string."0817

If the argVal is an array, we are going to say you have argument so-and-so as an array.0824

And then, we are going to use another foreach loop to loop over the array, and then output all of its different values.0831

And then, here we just make use of the is_null function to see if an argument value is null.0838

So here, we just have a called output type that is going to test all of these different scenarios 0845

and all of these different variable-type functions that we have.0850

We provide it with an integer 1, a float value 2.5, the string hello, the boolean value true, 0852

an array containing three integers (1, 2, and 3), and then the value null.0858

So, if we go ahead and load this page up, and we look at its output, we can see: argument 0 is an integer with value 1.0863

Argument is a float with value 2; notice that the first argument has that number 0--again, arguments in these built-in PHP functions 0874

(like func_get_args or func_num_args use a 0-indexing system, so that the first argument is 0 and the second argument is 1.0883

Here, it just outputs all of what we had input to the script; and it does something different 0895

for each argument that we pass into it, based on what type it is.0899

Here, we say it's a boolean--it is equal to true; this is an array, and then it has output the different values.0904

And so, for example, we could go and switch things around here: let's switch 2.5, and let's do two floats--get rid of that first integer.0909

We can see how the function can adapt; and so, when we run it again, it is going to say, 0922

"OK, your first argument is a float with value 2.5; your second was a float with value 3.3" and so on.0925

One other function that works with finding information about a variable's data type (actually, I'm going to erase this 0935

and clean things up a little bit)--all of these specifically work with defined data types in PHP: integers, floats, strings, bools, arrays, and null.0946

There is another is function that is is_numeric; and what it does is takes a variable that is typically a string 0957

and tells you true if it can be interpreted as a numeric value.0966

For example, it is used often on forms.0971

When you have a form on the Web, and you submit the form via GET (for example), it submits name/value pairs for the values that you entered.0974

Everything that is submitted is in a string, so even if it says, "How many children do you have?" and you entered the number 30983

on the form, and you submit it, it submits number of children equals 3, and 3 is a string--it is not a number.0990

It is not a number data type; it is a string data type.0996

And so, what is_numeric does is: you can pass it (for example) the string 3, and it is going to return true if that is a numeric string.1001

And if not, it is going to return false; so if you passed it the string a, which is not a numeric string, it is going to return false.1014

And it works for integers or floats; if you provide it 3.5, it is going to return true, because 3.5 can be interpreted as a number.1022

So, I have created another script here called variableTypeFunctions2.1031

And here, we have created a function called outputNumber, and it takes one required parameter.1039

And what it does is tests that parameter provided--if it is numeric.1045

If it is numeric (which means it is a string that can be evaluated as a number), it goes ahead and outputs saying "the number was so-and-so."1051

And if not, it is going to output a message saying that you need to provide a numeric string.1060

And what we have done is set up the form so that it accepts a GET parameter called number.1065

And here is our processing of the GET parameter number: it is what we have been using in our web application.1070

We create a short variable called number, and if the GET variable number exists, it gets set to the value; if not, it gets set equal to null.1077

Here we can actually see: we are using the is_null function, rather than using ==null.1086

We are saying, "If number, the short variable, is equal to null, then output a message 'you need to provide the GET variable number.'"1090

If not, it is going to call the function outputNumber with the value that was passed in as number, and then it runs this function.1098

And again, if it is numeric, it outputs the number; if not, it says "the GET variable number must be a numeric string."1105

If we go and take a look at the script, variableTypeFunctions2, it just calls it without any GET variables.1111

So, the first thing it is going to say is, "You need to provide a GET variable number."1118

So, if we go ahead and provide the GET variable number and set it equal to 101 (to be different), and we submit the script,1121

it is going to say "the number input was 101," because that is numeric.1130

We also could put in 101.5; that is a numeric number, even though it is a floating-point number.1134

It is going to say "the number input was 101.5."1140

What that means, in both of these cases: is_numeric returned true; 1143

that numeric string that you passed to the script via GET could be interpreted as a number.1146

Now let's say, instead, we set number equal to a string value, Hello.1154

We are going to get that error message that says "the GET variable number must be a numeric string,"1162

because this test is_numeric failed because it wasn't a numeric string, and so this message gets output.1166

For the homework challenge for today's lesson, I want you to create a function called multiply that has two required parameters: op1 and op2.1177

It is going to be similar to the add function that we developed in this lecture.1186

And then, it also accepts any additional number of numeric parameters.1191

The first thing you should do is test if op1 and op2 are numeric data types, using the is_numeric function that we just learned about.1195

If so, it multiplies them together; if not--if either one of them is not numeric--it should return the value false.1202

If op1 and op2 are both numeric--both required parameters are numeric--it multiplies them together.1210

And then, it goes ahead and, if there are any additional parameters (this is a variable-length parameter list function--if any 1215

other non-required parameters are provided), it is going to test each one to see if it is numeric.1223

And if it is, it is going to multiply them times the quantity that was the result of multiplying op1 times op2.1227

It is going to do that for each additional parameter, cumulatively multiplying.1234

If any of these additional arguments are non-numeric, it is going to return the value false.1238

So basically, all of the arguments supplied, including the two required arguments, are numeric, 1242

it is going to return the total from multiplying all of the numbers together.1248

And the way I want you to test that is: you can test your function multiply by inputting the following sets of inputs.1253

and then outputting the results of the multiply function, and then verifying that the results that you get are matched down here.1261

For example, if you input the two arguments 1 and 2, your result that you get (output) should be 2.1268

If you input 1 and the string Hello, your result should be false, because is_numeric is going to run on the string Hello, and it is going to return false.1278

If you input four different numbers (1, 2, 3, 4), those are all going to pass the is_numeric test, and the result is going to be 24.1285

If you pass in the two required parameters, and then two additional parameters (one of which is the string Hello),1292

the function should return false, because Hello is not going to pass the is_numeric test.1298

This is going to give you practice with the is_numeric function, which is something you are going to be using in your scripts a lot,1303

because again, when you take information from forms on the Web, they are always in the form of strings.1309

So, you need to run tests on them to see if they are numeric strings.1316

For example, it doesn't make sense to multiply strings together, so you would need to run these tests.1321

It is going to give you practice with that; it is also going to give you a little exposure to using user-defined functions with variable-length parameter lists.1325

That ends today's lesson; thank you for watching Educator.com.1333