Making Functions Re-usable (PHP Example)…

14 08 2006

Some how i been involved in creating few parts of http://www.hottdogzzbikinizz.com 😉 .
well i know what ya thinking , but its not like that *grin grin*…

One of the features i Developed was the JPG upload function [did’nt involve Mysql] …
what simply happens when you first submit the form it will check if you did’nt forget any field .
It will also check wether the submitted image is a JPEG/PJPEG otherwise it wont accept it …

the 2nd step is to process the uploaded image , Creates a folder with name-age-location of the submiter and stores the images in it.

3rd step is to send an email to the admin with all the user details , Image Location , and the actual Location( by ip ) of the submitter .

almost 300 lines of code ….

now lets see one of the examples :
imagine we want to create a folder for every user using his Name Age and Location assuming we already extracted the posted values into variables …

    $name
    $age
    $loc

$success = mkdir($name."-".$age."-".$loc,0777)
if(!$success) {

exit(‘something went wronge’);
}

this is ok , but what if i want to use the above code more than 100 times ?!
Copy & Paste …. ?
No ..

Lets see how can we make that re-usable …


function MakeDir($n,$a,$l) {$success = mkdir($n.”-“.$a.”-“.$l,0777);
if(!$success) {

exit(‘something went wronge’);
}
}

Now we can use that 1000 time with few lines of code ..
here is how …

MakeDir($name,$age,$loc);
MakeDir($name2,$age2,$loc3l);

//you can use a loop like [ foreach ( $x as $y ) etc …. ]

hope this was usefull …
now remember ..

Practice makes Perfect ...
🙂

Advertisements

Actions

Information

2 responses

13 11 2009
John Bait

Enjoyed reading through this site, I will send this site to a few of my friends

30 11 2010
yudhi

keren

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: