Tutorial
Introduction
This tutorial will teach you how to use includes and functions and it will explain to you why it's useful.
Step 1 - functions
What is a function and what do you use it for ?
Functions are very useful to do a specific procedure.
A way to simplify tasks you would like PHP to do more often than just once.
In the last tutorial you could see that we used a function to filter our text,
we made a function for this cos we will have to use that function every time
we are going to clear our user input.
Using a function would be alot easier than write the code down everytime you would
like to do that specific task.
We tried to make an image to explain to you how a function works:
You can see in the image we put the procedure into a function,
And the function will be performed 3 times to filter the text.
If we wouldnt have the function we would have to type the code three times in one script.
Step 2 - Functions and variables
A function cannot automaticly use the variables from all your scripts.
When you create a function, we will have to treat this function like an entire new
PHP file. There are no variables available yet.
If we do need variables in the function we are going to write... we will have to tell the function what that value will be.
Example:
Code:
<?php
function filter($string)
{
$string = ucfirst($string);
return $string;
}
$text = "hello this is a function test";
$text = filter($text); // We now give the variable $text to the function.
echo $text;
// Can you give more variables to a function?
// Yes you can, the function would look like this:
function filter2($string,$url)
{
$string = ucfirst($string);
$string = $string." ".$url;
return $string;
}
$text2 = "hello this is a my url:";
$link = "http://www.scripterplace.com";
$text2 = filter2($text2,$link); // We now give two variables to the function
echo $text2;
?>
|
Step 2 - Includes
What is an include?
An include is the PHP version of the html iframe, It's used to paste PHP files into other PHP files.
This is very useful because, you can store scripts on a certain place and use them on many different
places just like functions.
By using the include function you will only have to change a specific piece of code in one file
For example we will use the functions.php you could make for storing your functions.
We want to use the functions in index.php
What we will do is this:
Code:
<?php
include('../functions.php'); // The ../ will be the root in this case.
$string = "Put your text here";
$string = functions($string); // You can now use the function here
?>
|
You now know how to create functions and how to use them.
You also know how to include files and in what order PHP will read your files.
| « Previous | Next » |
Comments
Flamer - 2008-12-16
ucfirst is a standard php function which will capitalize every first character of the sentence. Meaning that it will read the first word (hello world, first word would be hello) and make it a capital H.
It will only read the first word of the entire string, so when you have something like "hello there. im flamer and i can script. how about you?" then it will only capitalize the H of Hello.
When using ucwords it will capitalize every first letter of every word in the sentence.
you can use these functions in combination with others, such as strtoupper() and strtolower().
Little example code:
Good work Remco!
PS: Maybe you should make the code box a scrolling div?? I can give you the phpbb3 code if you would like that?
It will only read the first word of the entire string, so when you have something like "hello there. im flamer and i can script. how about you?" then it will only capitalize the H of Hello.
When using ucwords it will capitalize every first letter of every word in the sentence.
you can use these functions in combination with others, such as strtoupper() and strtolower().
Little example code:
Code:
<?PHP
//UCFirst example
$string = "test. the word 'test' is capitalized!<br>";
$string = ucfirst($string);
echo $string;
//UCWords example
$string = ucwords($string);
echo $string;
?>
Good work Remco!
PS: Maybe you should make the code box a scrolling div?? I can give you the phpbb3 code if you would like that?
Flamer - 2008-12-18
I changed it, also i changed the sentence i was using.
Administrator - 2008-12-16
Btw, <br /> is XHTML.. just incase people get confused lol.
In these tutorials we use the HTML tag : <br>
They both do the same.. but you won't be able to validate your code properly if you use both of the tags in one script.
In these tutorials we use the HTML tag : <br>
They both do the same.. but you won't be able to validate your code properly if you use both of the tags in one script.
You have to be logged in to write a comment.



