Status
Not open for further replies.

litewarez

Active Member
1,367
2008
1
0
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:
/*
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
 
9 comments
to be honest i think before you attempt to learn new programming langs you should first understand what you want to become, for instance if you want to become a website developer then learn the following in order

*(x)HTML
*CSS
*XML
*Javascript
*PHP
*MySql
*ASP
*JAVA

But if you want to become a software engineer then you should first take a look at what OS you wish to program for i.e Windows, Linux, Mac, Other

then you should learn the architecture for the system and then learn the languages for that particular one

Personally Im a Web Developer and I have a job and a career set...

Peace
 
I personally prefer the naming convention of first letter small and others big just like javascript. Nice tut though :D
 
yea i usually write my vars and functions as HelloWorld but when im just bombing threw a quick script ill just do lowercase for speed lol
 
is there any advantage of using UpperCase?
i use for javascript, but just wondering if it has advantages/disad's using it in php?
 
EDITED**

with php it works as follows

1 character acts as a set amount of bytes so A and a have the exact same length and will not effect speed of your scripts

all tho if you are creating a script that others will read and edit for personal uses i would keep to industry standers and go with the flow of
lowercase first followed by lowercase until a new word then you use an uppercase to to show "visually" that there are several words.. for instance:

litewarezzoo => litewarezZoo
warezcoders => warezCoders
firstsecond => firstSecond

all tho some people tent to use _ "underscores" between words to help visability but this adds extra length to your script size and processing speeds....

peace
 
Status
Not open for further replies.
Back
Top