The real power of a programming language comes from its functions. Likewise in any programming language functions are fully part of the PHP programming language. It help access core actions of the language and also to extend it easily.
*Image Credit to http://mathinsight.org/exponential_function**
- What is a function?
To this question they are many answers all pointing out the same concept and idea. Here are some:
Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.
Source: http://www.cs.utah.edu/~germain/PPS/Topics/functions.html
A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.
Source: http://www.tutorialspoint.com/php/php_functions.html But practically what does that mean if your are beginner or you have never been in touch with functions?
To help you undertsand that let me take a real example of a bird. You know creating softwares/applications is similar to creating these beings. So imagine you have to create that bird, you will start by making it bones, flesh and parts, feathers, etc. upon that you will add the possibility for it to sing, walk, fly, jump, etc. This actions are the bird's functions(walk, jump, fly, sing).
Now that you have made your bird that way you need to press some buttons in order to make it execute an action(function). So for example whenever you want it to sing, all you have to do is to say sing
and it will, so on and so forth.
You can notice that those functions are common tasks to many birds and also they can be done over and over again by the same bird, when you created it only once.
Likewise this bird that's how you'll be facing cases in programming softwares, then instead of repeating them you will just have to encapsulate
them into a function so that you can use them whenever needed. An example of a case is that your software might be required to have the ability to calculate grades average for students.
Now let's see how to create a function.
- How is a function made/created
Now that you know what a function is, you might be wondering how to create one. Firs let me tell you in most programming languages we have two types of functions:
1. Built-In functions
Built-In functions are the ones that are already created for you by the creators of the programming language. No need to create them again, all you have to do is to call them when needed. Some built-in function in PHP are echo()
, print()
, time()
.
2. User-Defined functions
Those who created the programming language could not predict all functions we will need, or what what we will be doing. The built-in function are therefore just a part of it, you can decide to add to them you own-made functions, that's what is called User-Defined functions.
PHP has defined principles and rules in order to help you define User-Defined functions which complies to the languages syntax:
- A function should have a name, preferably a name which designates what the function does
- The name should start with a letter or underscore(_), but never a number or other special characters
- The name should not contain spaces
- The name should be preceded by the key-work
function
- The name should be immediately followed by brackets
(opening and closing)
- The brackets should be immediately followed by curly braces
{ }
, opening and closing - The logic of the function should be within the curly braces.
Read this for best practices in declaring functions: http://www.php-fig.org/psr/psr-2/#4-3-methods Following these rules, here is an example of our own user-defined function:
function functionName(){
//The function logic will be here.
}
- What is it used for?
All this what for? That might be what you are wondering about. Indeed, functions have many advantages and use. Already from what is said of the function up here you can see that it's a pain killer and helps create maintainable codes. But here are some other advantages:
- Reducing duplication of code
- Reuse of code
- Adding clarity to code
- Dividing huge problems into little pieces, so easily maintainable.
- Make it easier to reduce errors
- etc. You really see that when you face some particular problems
So this automatically answers the questions when and why you should use functions.
- How is it used?
Generally, once a function is declared, to use it you just have to call/invoke its name with the brackets like this:
<?php
functionName();
By calling the function this way it will run, which means its logic will be executed.
So to make it real let's do something. Create a new folder in your web root and name it functions
,within it create a new file and name it index.php
and add the following code in the index.php
file:
<?php
// Declaration of the test function
function testFunction()
{
echo " Test function was called! ";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Functions Tutorial - lancecource.com</title>
</head>
<body>
<h1>Functions tutorial</h1>
<?php
testFunction();
?>
</body>
</html>
Then run it with php -S 0.0.0.0:8888
or with you WAMP stack localhost url. You should just see Test function was called!
That's is it about functions. This looks very simple and basic. There more complex and complicated functions you may be called to create.
In the next part of this tutorial we will be going deeper in PHP functions, mainly we will be talking of functions with parameters(formal and actual, Optional Parameters and Default Values), function workspace, nesting functions, Functions Returning Values, and a bit of function programming in PHP.
Stay tuned for the next part by signing up to the news letter. And if you like this please share it with friends, sharing is caring.
Last updated 2024-01-11 UTC