00:00
00:00
Newgrounds Background Image Theme

gabmaster5 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Creating Functions

2,124 Views | 5 Replies
New Topic Respond to this Topic

Php: Creating Functions 2006-08-16 02:01:30


PHP: Main

PARTE 1: El Introduction
Have you ever been working on a project in PHP, and you do the same 10 lines of code over and over, to say, count the number of rows in a MySQL database, and return a string if empty? Well, I have, and I have to say, I wished there was a function that could do that.

But there is!

PHP framework includes a way to create your own functions that you may have wished were in PHP already. Let's dive in to the fast-paced, action-packed world of creating functions!!! (did I grab anyone's attention yet?)

PART 2: How It Works
So, you may ask, how does it work? Well, as you can plainly see, the title of this section is "How It Works". So be patient, and I'll get there.

Now, if you have a basic knowledge of PHP, or even if you've just dabbled in it, you know how the functions work. There's the one-argument functions such as htmlspecialchars(), and there are multiple-argument functions such as mysql_connect(). That's exactly how you're going to create your function.

Here's a sample function I made called "mysql_full_connect()".

function mysql_full_connect($a, $b, $c, $d) {
mysql_connect($a, $b, $c);
mysql_select_db($d);
}

If that is in the PHP page, I could type up something like this...

mysql_full_connect("localhost", "user", "password", "site_db");

Instead of having to type all this...

mysql_connect("localhost", "user", "password");
mysql_select_db("site_db");

You may be thinking, "Hey, that's just a few characters, nothing big. Why should I use this?" or perhaps, "Wait...what?" Well, I'll be answering both these questions, and maybe even more. Let's start with question 2.

PART 3: Wait...what?
Alright, let's break down how this works, using my example from before. I'll be breaking this down THOUGHT BY THOUGHT, not line by line.

function mysql_full_connect

This will name the function I am going to create. This name will be the name I use to activate the function.

($a, $b, $c, $d)

These are variables...kinda a no brainer, but these variables represent the arguments you are going to put into the function. As the function is created, you use these variables to tell PHP what you want to do with the arguments in that slot.

{

This opens the area to create the function.

mysql_connect($a, $b, $c);

As you can see, I'm calling to the function mysql_connect(). The arguments are variables that I put in the created function. Making sense so far?

mysql_select_db($d);

Here, I'm calling to the mysql_select_db() function, using $d as the argument. That means, when this process is used, whatever is in the argument slot that $d is in will be put in this function.

}

This closes off the function.

So, here's a quick recap of what I just did. I just made a function that works as a 2-in-1 action, using premade PHP functions. Ta da. Now, you may wonder...when does that come in handy? Well, I'll tell you in the next section - "Where This Comes In Handy".

PART 4: Where This Comes In Handy
If you're like me, you don't want to write down two functions to connect to your database. It's alot easier to write...

mysql_full_connect("localhost", "user", "password", "site_database");

than it is to write...

mysql_connect("localhost", "user", "password");
mysql_select_db("site_database");

So, there's a simple way to do that without creating the function every page. Simply put your function creation in a page called "functions.php". Then require it on every page, at the beginning. Simple. Also, if you want to connect to the same database even easier, just make the function use your arguments. Such as...

function connect_database() {
mysql_connect("localhost", "user", "password");
mysql_select_db("site_database");
}

Then, when you call on the connect_database() function, it will connect with even less typing for you.

Another reason this can be good. Let's say your host has PHP4 installed, and you want to run a new function such as...file_put_contents. This only comes in PHP5, so let's make the function, eh?

function file_put_contents($a, $b) {
$handle = fopen("$a", "w");
fwrite($handle, "$b");
fclose($handle);
}

There you go, $a represents the file and $b represents your string. (disclaimer: i didn't test this function)

PART 5: The Conclusion
There are infinite possibilites to this, I only brushed over the tip of the iceberg. To go more in depth, it's best to work with it yourself for a while.

I hope you liked the tutorial. If you didn't...I hope you'll tell me why.

Response to Php: Creating Functions 2006-08-16 02:11:59


Very nice tutorial, but we actually have two tutorials on creating functions.

That's perfectly fine though. It's good to have more than one thing because it's presented in different ways.

Anyway, I like it!

Response to Php: Creating Functions 2006-08-16 02:25:58


I didn't see it :(

I thought I was being original...

Response to Php: Creating Functions 2006-08-16 02:32:33


At 8/16/06 02:25 AM, WoogieNoogie wrote: I didn't see it :(

I thought I was being original...

No, but it's great! There's nothing wrong with having multiple tutorials on one subject. You could always use more information. It's a great tutorial.

Response to Php: Creating Functions 2006-08-16 05:50:10


At 8/16/06 02:01 AM, WoogieNoogie wrote: function mysql_full_connect($a, $b, $c, $d) {
mysql_connect($a, $b, $c);
mysql_select_db($d);
}
function connect_database() {
mysql_connect("localhost", "user", "password");
mysql_select_db("site_database");
}

just so im sure this can be done. lets say teh second function there is like, ofr a defult database, but you sumtimes want to connect to other ones, jsut to make life that bit easyer, i think. can you right the function like so

function connect_database($a $b $c $d){
if ($a = "" && $b = "" && $c = "" $d = ""){//testing if there were no variables given
$a = "localhost" //etc etc
}
mysql_connect($a, $b, $c);
mysql_select_db($d);
}

that should work so that you can either go

connect_database() // conneccts to defult database

conect_database(variables for difrent database) // connect to difrent database

should work right?

Response to Php: Creating Functions 2006-08-16 12:09:32


At 8/16/06 05:50 AM, thecoshman wrote: should work right?

I'm not sure, but I don't think it would. I'm thinking it would echo back that the required arguments aren't supplied. Because, it might be looking for...

mysql_full_connect("", "", "", "");