PHP-and-MySQL/C2/Functions-Basic/English-timed

From Script | Spoken-Tutorial
Revision as of 12:05, 21 May 2015 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
Time Narration
00:00 Welcome to the Spoken Tutorial on the Basic Function. This is one of the two tutorials on this topic.
00:06 In this tutorial, I'm going to take you through how to create your function, the syntax of it and how to input one or more than one value.
00:13 The other tutorial will be on returning values.
00:17 So, let us start with this. I am creating my PHP tags here. I'll start with my syntax which is function.
00:23 And then the function name which is myName.
00:27 It's probably easy to use common caps here. Which is why you have lower case, then going to upper case, back to lower case. Finally, the new words will start from upper cases.
00:38 It's much easier to read but I always prefer to use the small case.
00:43 Then you're going to have 2 parentheses; nothing inside them yet. We're not taking any input here and inside I'll write my code. So I'll write "Alex".
00:56 Okay. If we run this now, we will see that nothing happens.
01:05 Because of that we've declared our function. But we haven't called it yet.
01:11 Now, to call our function, we just need to write the name of the function, the 2 brackets and the line terminator.
01:18 If we are putting values through this that need to be processed, we'll put them in here.
01:24 But, for now don't worry about that. We're just calling out our function which will execute this block of code.
01:30 So, let's do a Refresh and there you go. "Alex" has been echoed down.
01:36 Now suppose, if I want to add more than one line of code, I can put as much code here as I want. That's what the block is there for; to accommodate for more new lines. Let's just test that.
01:53 We can see that it worked. Another thing to add is that it does not need to be called on it's own. It can be called, for example, my name is 'myName'.
02:13 All-right. We need to say 'my name is' and then echo the function separately.
02:22 The reason this didn't work is probably because this is not a value. This is a function, so it's already going to echo "Alex".
02:36 So, for picking out a new line it'll be the same, saying echo my name is, echo "Alex", Okay?
02:45 So, this would not work if we had it here. For example, you would literally just have output my name is, my name, Okay?
02:57 So, let's take this back down here. Refresh it, there you go, "my name is Alex".
03:03 Just to make sure that I'm clear. If I was to replace this with a code which was being executed, it would look like that.
03:11 So, we wouldn't do that, Okay?
03:16 So, that was only to get clear on that. Now we'll move on to the fact that you can call a function before it's been defined. That is, because of the way PHP works. So if I say, refresh this, you'll expect this because the function is being called before it's been declared. That it would recognize it from top to bottom.
03:46 However, it doesn't work that way. You can declare it at the bottom of the page if you think that's best. I always prefer to declare at the top so that I can resume or back up to the top and see where I am.
04:00 But, that's about it. Now, putting a value in, what I'll do is I'll say "your name is" name. That's echoing out "your name is" and then the variable name. I'll name the function yourname.
04:19 Now, where's the variable going to come from? I want the user to be able to input this. I mean I'm not talking about input but if I put the name here and then I say your name, "Alex".
04:39 This is how it works. yourname calls the function, takes this variable into account, puts this variable into name and then reads the variable from echo. This is what I mean.
04:58 So, we're basically saying 'your name Alex'. To proceed with, I need a value for this, particularly a string value. So you go up to here and see if anything has been entered as input. You see it has been. It's "Alex". So we should get 'your name is Alex' now.
05:17 There we are! We can change this to "Billy". Thus you've seen how it works.
05:26 All-right. Now what I want to do is, I might want to add to my function to say that you are so and so years old. So, I can say that you are age years old.
05:38 What we need to say is name and age. Basically, what we do is add another variable.
05:50 Okay, so, we're adding an extra thing here, separated by a comma. Here we need to separate the variables by a comma. So, again it's taking this variable into account, putting it here and echoing it out here. Taking this variable into account. Putting it in the call and echoing it out here.
06:10 This is basically the structure of your variable. How many variables it takes and this is how you code your function.
06:19 So, let's test that. Okay, you need a space out there. I can change this again to "Alex", 19 and Refresh. There you go.
06:31 So, functions are written to save time. It takes large blocks of code. It can take an input. It will process it in such a way that it would be time consuming otherwise.
06:46 This brings us to the end of this tutorial. For advanced functions like 'returning value', please check the other tutorials on functions.
06:55 Thanks for watching. This is Arvind for the Spoken Tutorial Project. Goodbye.

Contributors and Content Editors

Minal, Pratik kamble, Sandhya.np14