Locked lesson.
About this lesson
How to create and use functions in PHP.
Exercise files
Download this lesson’s related exercise files.
Functions.docx59.1 KB Functions - Solution.docx
59.7 KB
Quick reference
Functions
Functions are like programs inside of our program.
When to use
Functions allow us to reuse code.
Instructions
Functions only run if you specifically call them. Otherwise they are ignored by the normal flow of our code.
Functions take this form:
function my_function(){
// do something;
}
To call a function:
my_function();
You can pass parameters into a function when you call it:
my_function("John", "Elder");
Hints & tips
- functions are programs inside your program
- functions only run if you specifically call them
- You can pass parameters into a function when you call it
- 00:05 In this video, I wanna talk about functions.
- 00:07 Now PHP has over 1,000 built in functions, and
- 00:09 we're gonna look at some of those a little bit later on.
- 00:11 But in this video, I wanna talk about creating your own functions.
- 00:15 A function is basically a little program inside of your program, and
- 00:19 it doesn't get run immediately when your program runs when your page runs.
- 00:23 When something comes to your website, whatever,
- 00:25 a function only gets to run only if you call it specifically within your program.
- 00:29 We're gonna look at that now.
- 00:30 So to create a function, it's pretty simple, we just type in function, and
- 00:35 then give it a name, and let's call this mathing.
- 00:38 We're gonna do some math.
- 00:40 And then you do these two parenthesis, and
- 00:43 then your squiggly bracket thing and that's it.
- 00:45 So we now have a function, now there's nothing actually in this function.
- 00:49 We would do something inside of here, to do something,
- 00:54 and we'll do that in just a minute.
- 00:55 Now to call this function you just type the name of it, mathing,
- 00:59 the little (), and of course your ;.
- 01:02 Now, this calls the function.
- 01:07 Let's actually do something here.
- 01:09 Let's write a function.
- 01:11 And let's say echo 2 + 2, if we remove this lines,
- 01:16 save this, come back here and hit reload, nothing happens.
- 01:19 Because like I said, with the function, it only runs if you call it.
- 01:23 So to call it, we just name it, and if we save this, hit reload, boom, we get four.
- 01:28 Now, we can actually pass things in to a function from the outside world, and
- 01:33 those are called parameters.
- 01:34 We can pass a parameter in.
- 01:35 Let's say we wanna pass this in a couple of numbers, and do some math with it.
- 01:39 So let's call this num1, and num2.
- 01:44 Inside of this program, we can use these variables.
- 01:47 So let's go num1 + num2;.
- 01:53 Now we've set up our function to take in parameters,
- 01:56 in order to then send parameters to our function, and in order to send numbers,
- 02:00 we put them right here, when we call the function between these two parenthesis.
- 02:04 So if we had 2 and 3, the number 2 becomes this num1 variable,
- 02:10 get shoved into this variable.
- 02:13 And number 3, get shoved into this num2 variable, and
- 02:16 then get passed into our function, save this.
- 02:18 And if we look at our program, it's just going to echo out number 1 + number 2,
- 02:22 which in this case is 2 and 3.
- 02:24 If we've done our job correctly, we should get 5, so hit reload and boom, 5.
- 02:28 And of course you can change these around for 41 and 3, 44.
- 02:33 It's not just numbers, and you can pass absolutely anything
- 02:38 in if we wanted to concatenate instead of add, and if we wanted
- 02:43 to do strings instead of numbers, we could easily do John and Elder, now.
- 02:49 Num1 becomes John and num2 becomes Elder, and
- 02:52 it will concatenate these two together and out put the JohnElder, there you go.
- 02:58 We could also do, put a space in there, whatever you want,
- 03:02 you get the idea, John Elder.
- 03:04 Functions are very important and they're very powerful.
- 03:06 They're like little programs within your programs.
- 03:09 And remember, they don't get run automatically,
- 03:11 they only get run when you call them.
- 03:13 And the cool thing is, you can call them over, and over, and over again.
- 03:16 I mean, whenever you want from now on, you can call this function.
- 03:20 So if we can quote three more times, and we get boom, boom, boom, three more times.
- 03:26 We could, let's see, up here, play around with this a little bit.
- 03:30 Add a line break just for fun.
- 03:32 Boom, Boom, Boom.
- 03:35 So you see, very powerful.
- 03:37 Every programming language has functions.
- 03:39 Some programming languages call them methods.
- 03:41 I know in Ruby, they call them methods.
- 03:43 But in PHP, they call them functions, and it's just a fundamental part of
- 03:46 programming, and you'll use functions all the time.
- 03:48 So in the next video, we're gonna look at random numbers and dates.
Lesson notes are only available for subscribers.