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.