Tutorial
Click on thumbnailed images to enlarge
T.K.O. was kind enough to write the [url=http://www.createblog.com/forums/index.php?showtopic=83504]PHP: Introduction to Functions[/url] tutorial for all of us. Unfortunately, he has not had time to cover using arguments in functions, so I thought that it might be handy to have a tutorial on that, too.
First of all, please make sure you have read the [url=http://www.createblog.com/forums/index.php?showtopic=83504]PHP: Introduction to Functions[/url] tutorial. I am going to reference some things in that tutorial, and I won't go into as much detail as T.K.O (to avoid being redundant). Please make sure you fully understand that tutorial before proceeding.
Okay. So after completing T.K.O.'s tutorial, you have a nice function that will show a header on your page. But let's say that you want a function that will set the title of the page, and create a header with that title. Of course, each of your pages has a slightly different title! How do you go about using T.K.O.'s [font=Courier]showheader()[/font] function while still dynamically setting a title?
The answer is in function arguments. Arguments are variables, strings, or numbers that are sent to a function in order to alter its behavior. Let's say that after you followed T.K.O.'s tutorial, your [font=Courier]showheader()[/font] function looked like this:
You really like that function and start using it on all your pages. But, oh no, that means that every page will be called "My Site :: This Page" and having a header that says "This Page". You don't want that! You want "This Page" to be set to the appropriate title for the page.
So how do you do that? Well, first, you modify your function so it will accept an argument, like this:
Note that in the parentheses, the function now takes the variable $title. But how do we use this variable? Well, like this:
PHP will now replace $title with the text you send the function. But how do you actually pass a value to the function?
Well, in T.K.O.'s tutorial, you learned that to call a function, you use this:
Sending a function a value is similar, but you include the value in parentheses, like so:
Your pages will now be labelled with "That Title". Note the use of quotes when sending the string.
You can also set a variable in your code, and send the function that variable:
Your pages will now be labelled with "A Different Title". Note that, when sending a variable to a function, you do not use quotes.
Finally, let's say that you want to send two values: a page title, and a label for the header. That's easy to do! First, modify your code like so:
Note that you now have two variables in your function: $title and $header. You call the function the same way as before, but pass it two arguments, separated by commas, in the order you listed them in the function. Let's say you want the title to be "This Page", and the header to say "My Awesome Page":
That's it! You're now able to dynamically change your header to whatever title you want!
Questions? Comments? Room for improvement? Post below!
First of all, please make sure you have read the [url=http://www.createblog.com/forums/index.php?showtopic=83504]PHP: Introduction to Functions[/url] tutorial. I am going to reference some things in that tutorial, and I won't go into as much detail as T.K.O (to avoid being redundant). Please make sure you fully understand that tutorial before proceeding.
Okay. So after completing T.K.O.'s tutorial, you have a nice function that will show a header on your page. But let's say that you want a function that will set the title of the page, and create a header with that title. Of course, each of your pages has a slightly different title! How do you go about using T.K.O.'s [font=Courier]showheader()[/font] function while still dynamically setting a title?
The answer is in function arguments. Arguments are variables, strings, or numbers that are sent to a function in order to alter its behavior. Let's say that after you followed T.K.O.'s tutorial, your [font=Courier]showheader()[/font] function looked like this:
function showheader() {
echo '<html>';
echo '<head><title>My Site :: This Page</title></head>';
echo '<body>';
echo '<h1>This Page</h1>';
}
echo '<html>';
echo '<head><title>My Site :: This Page</title></head>';
echo '<body>';
echo '<h1>This Page</h1>';
}
You really like that function and start using it on all your pages. But, oh no, that means that every page will be called "My Site :: This Page" and having a header that says "This Page". You don't want that! You want "This Page" to be set to the appropriate title for the page.
So how do you do that? Well, first, you modify your function so it will accept an argument, like this:
function showheader($title) {
echo '<html>';
echo '<head><title>My Site :: This Page</title></head>';
echo '<body>';
echo '<h1>This Page</h1>';
}
echo '<html>';
echo '<head><title>My Site :: This Page</title></head>';
echo '<body>';
echo '<h1>This Page</h1>';
}
Note that in the parentheses, the function now takes the variable $title. But how do we use this variable? Well, like this:
function showheader($title) {
echo '<html>';
echo "<head><title>My Site :: $title</title></head>";
echo '<body>';
echo "<h1>$title</h1>';
}
echo '<html>';
echo "<head><title>My Site :: $title</title></head>";
echo '<body>';
echo "<h1>$title</h1>';
}
PHP will now replace $title with the text you send the function. But how do you actually pass a value to the function?
Well, in T.K.O.'s tutorial, you learned that to call a function, you use this:
showheader();
Sending a function a value is similar, but you include the value in parentheses, like so:
showheader('That Title');
Your pages will now be labelled with "That Title". Note the use of quotes when sending the string.
You can also set a variable in your code, and send the function that variable:
$page = 'A Different Title';
showheader($page);
showheader($page);
Your pages will now be labelled with "A Different Title". Note that, when sending a variable to a function, you do not use quotes.
Finally, let's say that you want to send two values: a page title, and a label for the header. That's easy to do! First, modify your code like so:
function showheader($title, $header) {
echo '<html>';
echo "<head><title>My Site :: $title</title></head>";
echo '<body>';
echo "<h1>$header</h1>";
}
echo '<html>';
echo "<head><title>My Site :: $title</title></head>";
echo '<body>';
echo "<h1>$header</h1>";
}
Note that you now have two variables in your function: $title and $header. You call the function the same way as before, but pass it two arguments, separated by commas, in the order you listed them in the function. Let's say you want the title to be "This Page", and the header to say "My Awesome Page":
showheader('This Page', 'My Awesome Page');
That's it! You're now able to dynamically change your header to whatever title you want!
Questions? Comments? Room for improvement? Post below!
Tutorial Comments
No comments yet. Be the first one to comment!