PHP Functions

PHP is actually a very complex programming language, so there is a lot more to it than simple commands that are used in CSS or tag names used in HTML.

Although it would be nearly impossible to completely explain all of the fundamentals of PHP without writing a multi-hundred page book, there is one more basic PHP concept that I need to explain to you: functions.

A PHP function is similar to a command except that it accepts arguments that can change the way it works. Think of an argument like an HTML tag attribute and value - this is similar.

The first thing that you need to understand about PHP functions is that there are some that already exist in the programming language for you to use, but then you can also create your own to use in your code!

If you are ever looking for specific information on how to do something with PHP programming, search here: http://php.net/

That is the official website for the PHP programming language.

I have personally been using this website almost every day for more than a decade!

This is one of the things that a lot of people don't initially understand about programming - you don't have to know how to do everything! The real key to learning how to program is to understand the fundamentals and know where to get quick help to figure out how to do what you want to do.

If you want to do something with your PHP code and don't know how to do it, just search on php.net for a function that will help.

Even once you know how to write PHP code, you can still use the site as a reminder on how to use specific functions. If I had to tell you the exact order of the arguments of every single PHP function that exists, I simply couldn't do it, despite all of my experience with it, but the truth is that I don't need to know all of that because that information is easily available on that website.

If you work your way through programming a script one function at a time by using that website, you will quickly find yourself able to write a wide variety of scripts without having to reference the site.

PRINT_R Function

My intention in this training programing is NOT to simply go through a long list of PHP functions for you guys because I want to show you a practical example of using PHP in WordPress so you can actually see a real-life situation of how it can help your websites.

However, I do want to mention one function real quick, just to demonstrate one of the internal PHP functions.

This function DOES NOT actually have a good practical use, at least not for live websites, although I personally find myself using it all the time when writing code to help with debugging (which is the real practical use for it, in my opinion).

The function I am talking about is called PRINT_R. This function outputs the contents of an array to the browser.

Earlier in this tutorial, you may remember me showing you an example with an array. When I wanted to output the values in the array, I had to reference the values by their key names.

With PRINT_R, I do not need to use the key names - I simply provide the variable name for the array.

Here is an example of this function using the array that I had created in one of the earlier examples for arrays:

<?php
//Declaring the array with key names and values
$a = array('first_key' => 'Hello', 'second_key' => 'Hello Again');

//Now I output the contents of the array to the browser print_r($a);
?>

This example outputs the following to the browser:

Array
(
[first_key] => Hello
[second_key] => Hello Again
)

In this example, I first create my array and store it in the variable $a. Then, I use the PRINT_R function to output the contents of the array to the browser.

To use the PRINT_R function, I simply use an opening parenthesis, then the array variable that I want to output, then a closing parenthesis, and finally a closing semicolon.

Create Your Own PHP Functions

Now that I have shown an internal function, I want to show you how you can create your own PHP functions.

Before I get into that, it is good to know why someone would want to create a PHP function in a script.

If you remember from my CSS lesson I talked about code bloat. The same thing applies to PHP code. You can actually consolidate your code into functions instead of using a piece of code over and over again.

Going back to my example with the IF, ELSEIF, ELSE statement and the FOR statement, I want to show you how this could be converted into using a function instead.

In that example, I was using an IF, ELSEIF, ELSE statement to test the value of a variable. What if I wanted to use that same testing statement numerous times through a script I was writing? I could simply copy and paste the code from the inside of the FOR statement and reuse it over and over again as needed, but this would create code bloat.

Beyond code bloat, there are a few more good reasons to use functions. First, it will help you stay more organized, but beyond that, it can save you a ton of work. If I ever decided that I needed to make a change to part of my code and that code was reused over and over again throughout my script, I would have to find each use and make changes to each one. As you might be able to imagine, this takes a lot of time and is tedious work that isn't very fun to do!

Creating your own functions helps you avoid all of this. Below I have reworked the FOR statement example using a function instead:

<?php
function process_my_value($a)
{
if ($a == 1)
{
echo 'The variable 'a' is equal to 1.<br />'; }elseif ($a == 2){
echo 'The variable 'a' is equal to 2.<br />';
}else{
echo 'The variable 'a' is not equal to 1 or 2!';
}
}
for ($a = 1; $a <= 3; $a++)
{
process_my_value($a);
}
?>

In the example above, I get the exact same output that I received the first time around, but this time, I have moved the code that evaluates the value of $a and echoes text to a PHP function that I have created!

I start by using the function command.

Next, I provide a custom name for the function I want to create. It is VERY important to ensure that this function isn't already used or else you will get a PHP error saying that the function is already declared.

One tactic to avoiding duplicate function names is to create your own unique prefix to use for all of the functions you create in a particular script. This way, you can use short or easy to understand names after the prefix and not worry about whether they are already function names (because you have the prefix before them).

After the custom function name, I use an opening and closing parenthesis. Everything between those parenthesis are my function arguments. All you do is provide a variable name that will be used for that argument value in the function.

In this example, I have provided the variable name $a for the function argument. If I had more than one argument, I would just split each with a comma.

I then use curly brackets to enclose the code for my function - everything between them is the code that is only run when the function is called.

After the function is declared, I have my FOR statement that I used before. Instead of a bunch of code inside of it this time, I simply call the function.

To call the function, just use the name of the function and then put the value inside of the parenthesis (and close with a semicolon). It really looks just like the code used to create the function but without the actual function command.

Now each time my FOR statement loops through, it simply passes the value of $a to the function, when then processes it and outputs the appropriate text.

Although the end the result here is the same, the big difference is that I could continue to use that same function over and over again in my script instead of duplicating the code in the function.


© PHP Training Guide

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (24.08.2014)
Views: 373 | Rating: 0.0/0
Total comments: 0
avatar