Difference between revisions of "PHP-and-MySQL/C2/Functions-Basic/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{|Border=1 !Time !Narration |- |0:00 |Welcome to the Spoken Tutorial on the Basic Function. This is one of the two tutorials on this topic. |- |0:03 |In this tutorial, I'm going…')
 
 
(No difference)

Latest revision as of 21:59, 1 December 2012

Time Narration
0:00 Welcome to the Spoken Tutorial on the Basic Function. This is one of the two tutorials on this topic.
0:03 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
0:12 The other tutorial will be on returning values
0:16 So let us start with this. I am creating my PHP tags here. I'll start with my syntax which is function
0:26 And then the function name which is myName
0:28 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
0:40 It's much easier to read but I always prefer to use the small case
0:43 Then you're going to have 2 parenthesis. Nothing inside them yet. We're not taking any input here and inside I'll write my code. So I'll write Alex
0:56 Okay, if we run this now , we will see that nothing happens
1:05 Because of that we've declared our function. But we haven't called it yet
1:07 Now, to call our function we just need to write the name of the function, the 2 brackets and the line terminator
1:16 If we are putting values through this, that need to be processed, we'll put them in here
1:20 But, for now don't worry about that. We're just calling out our function which will execute this block of code
1:26 So, let's do a refresh and there you go. Alex is been echoed down
1:30 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
1:50 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 'my name'.
2:10 All-right, we need to say, 'my name is', and then echo the function separately
2:22 The reason this didn't work probably is, because this is not a value. This is a function, so it's already going to echo Alex
2:32 So, for picking out a new line it'll be the same, saying echo, my name is, echo Alex, Okay?
2:42 So, this would not work if we had it here. For example, you would literally just have output my name is, my name, Okay?
2:49 So, let's take this back down here. Refresh it, there you go, my name is Alex
2:58 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
3:07 So, we wouldn't do that, Okay?
3:12 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
3:40 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
3:50 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'
4:13 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
4:35 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.
4:50 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
5:08 There we are! We can change this to Billy. Thus you've seen how it works
5:20 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 you are age years old
5:36 What we need to say is name and age. Basically, what we do is add another variable
5:43 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
6:00 This is basically the structure of your variable. How many variables it takes and this is how you code your function
6:09 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
6:25 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
6:34 This brings us to the end of this tutorial. For advanced functions, like returning value, please check the other tutorials on functions.
6:42 Thanks for watching. This is Arvind for the Spoken Tutorial Project. Goodbye.

Contributors and Content Editors

Chandrika, Pravin1389