Heya again,
ok so this time ive decided to show you how to create functions and discuss the pros and cons of this..
A function is a block of code the performs a certain task, such predefined functions your are probably familiar with such as
but if you want to perform a task several times throught a webiste you dont wna tto have to write the code every time you need to perform it.
so functions are a way to create a block of code that is stored in the memory of php and can be called over and over again.
Heres how you create a function:
Well hope you understood it and please comment if you like
Peace
ok so this time ive decided to show you how to create functions and discuss the pros and cons of this..
A function is a block of code the performs a certain task, such predefined functions your are probably familiar with such as
- trim()
- print()
- nl2br()
but if you want to perform a task several times throught a webiste you dont wna tto have to write the code every time you need to perform it.
so functions are a way to create a block of code that is stored in the memory of php and can be called over and over again.
Heres how you create a function:
PHP:
/*
A function has to have the following.
1.A Name
2.Variables (if any).
to create a function the first word should be the word function and then followed by the name of the function your making
function MyFunctionName
and then followed by brackets like so
function MyFunctionName()
and then finally it should have a pair of braces like so
function MyFunctionName(){}
If we add this to a php script it will work but its useless as we have only defined a function and not done anything with it yet so lets do that now.
*/
//#1
function MyUrl(){
return 'http://www.litewarez.com';
}
#2
function MyUrl(){
echo 'http://www.litewarez.com';
}
/*
Ok so as you can see theres a difference beetween #1 and #2 ...
#1 only returns the data to the origin it was called
#2 this will print the data to the origin it was called
*/
//Lets call them
MyUrl(); // you will not see anything on your screen as #1 is returned
myUrl(); // this will print your url to the screen as you have echo'ed it within the function
/*
Its always better to return data from a functions as this allowes you to
move data around the script without any of the users seeing the data
on there screen
*/
/*
Ok so lets work with parameters.
Parameters allow you to send a string or an object directly into the function to process, below are some examples.
*/
function MakeBold($String){
return sprintf("<strong>%s</strong>",$String);
//this will replace %s with your $string
}
//Usage
$Plain_string = "I Love Litewarez.com";
$Bold_string = MakeBold($Plain_string);
//Plain_string = I Love Litewarez.com
//Bold_string = <strong>I Love Litewarez.com</strong>
Example 2.
//heres an example of performing more complex code within a function
function GetWebData($url = NULL){
if(strlen($url) < 5){
return "Url too short";
}
if(function_exists("file_get_contents")){
$Website_data = file_get_contents($url);
}else{
return "Funtion (file_get_contents) does not exists. please upgrade php";
}
if(strlen($Website_data) != 0){
return $Website_data;
}else{
return "0 Bytes of data retrieved from " . $url;
}
}
//Now you can just use this command to grab data from a website wherever
//and whenever you need to perform this task
$Grabbed_html = GetWebData("http://www.SomeSite.com");
/*
You can use this as many times as you wish and if you want to read more
about creating functions just google "Create php functions"
*/
Well hope you understood it and please comment if you like
Peace