Welcome to the Spoken Tutorial on the Basic Function. This is one of the two tutorials on this topic.
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
The other tutorial will be on returning values
So let us start with this. I am creating my PHP tags here. I'll start with my syntax which is function
And then the function name which is myName
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
It's much easier to read but I always prefer to use the small case
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
Okay, if we run this now , we will see that nothing happens
Because of that we've declared our function. But we haven't called it yet
Now, to call our function we just need to write the name of the function, the 2 brackets and the line terminator
If we are putting values through this, that need to be processed, we'll put them in here
But, for now don't worry about that. We're just calling out our function which will execute this block of code
So, let's do a refresh and there you go. Alex is been echoed down
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
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'.
All-right, we need to say, 'my name is', and then echo the function separately
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
So, for picking out a new line it'll be the same, saying echo, my name is, echo Alex, Okay?
So, this would not work if we had it here. For example, you would literally just have output my name is, my name, Okay?
So, let's take this back down here. Refresh it, there you go, my name is Alex
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
So, we wouldn't do that, Okay?
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
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
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'
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
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.
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
There we are! We can change this to Billy. Thus you've seen how it works
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
What we need to say is name and age. Basically, what we do is add another variable
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
This is basically the structure of your variable. How many variables it takes and this is how you code your function
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
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
This brings us to the end of this tutorial. For advanced functions, like returning value, please check the other tutorials on functions.
Thanks for watching. This is Arvind for the Spoken Tutorial Project. Goodbye.