Results 1 to 10 of 10
Threaded View
-
9th May 2009, 05:20 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comPHP Tutorial ( Creating Functions ).
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
- 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 Code:
/*
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"
*/
Peacelitewarez Reviewed by litewarez on . PHP Tutorial ( Creating Functions ). 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 trim() print() nl2br() Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Tutorial for Creating Vbulletin Theme ?
By warezrock in forum Tutorials and GuidesReplies: 7Last Post: 27th Apr 2011, 09:25 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 4)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 9th Feb 2010, 04:24 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 1)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 30th Jan 2010, 03:37 PM -
PHP Tutorial (Creating Classes)
By litewarez in forum Tutorials and GuidesReplies: 0Last Post: 14th May 2009, 11:40 AM -
Tutorial - Creating Signatures in Photoshop
By rich5lee in forum Tutorials and GuidesReplies: 0Last Post: 28th Jul 2008, 05:14 PM
themaCreator - create posts from...
Version 3.53 released. Open older version (or...