Posted by : senan Wednesday, February 12, 2014

Welcome! In this section we are going cover PHP functions. But first...

What are Functions?

 

In technical terms, a function is a group of instructions to perform a task. In layman’s term, a function is a block of code with a name which can be used in our code whenever we need it.

The concept might be new to you but when you look at what a function is, you will be amazed how useful and simple it is to create a function. So, let's have a look at what a function looks like...

Function Syntax
function my_function_name(parameters)
{
 //block of code
}

A typical function has three components to it

  • Starts with the word function
  • Has a function name followed by parentheses (). A function name can only start with a letter or a underscore.
  • The code block in the function always goes inside curly brackets.
Now, let's look a how we create functions in PHP.

A Simple PHP Function

Example - Creating and Calling a PHP Function

<html>
<body>

<?php
//we create a function name my_function
function my_function()
{
 echo "Hello! How are you?";
}

//we call our function like this when we want to use it
my_function();

?>

</body>
</html>
In the above code, we creat our own function name my_function() and we call that function by just typing the its name. The above code outputs "Hello! How are you?".

PHP Function with Parameters

A function can be used to pass arguments (values or information) into it through parameters. This is useful when we want to add more functionality to our functions. For example, we can modify the above function to pass a person's name and a message into it and display that information.
Note: Parameters appear within the parentheses "()". They are like normal PHP variable.

Example - PHP Functions with Parameters

<html>
<body>

<?php
//we create a function named my_function
function my_function($first_name, $last_name, $message)
{
 echo "$first_name $last_name once said " . $message;
}

//we call our function like this when we want to use it
my_function("Duke", "Nukem", '"It\'s time to kick some ass and chew bubble gum."');

?>

</body> 
</html> 
In the above example we created a function with three parameters, $first_name, $last_name and $message. We used these parameters to send our custom arguments (values) to our function. The above example outputs... Duke Nukem once said "It's time to kick some ass and chew bubble gum.".
Note: In our function we can make as many parameters as we want. Parameters appear within the parentheses "()". They are like normal PHP variable.
Hopefully, now you're are beginning to see the use of functions and realizing how useful they are.

PHP Functions with Return value

We can have functions to return a value. So, instead of echoing or printing value inside the function, we can have the function return that value. That value can be anything. It can be a string, an ineteger, a flaot value, an array or an object. We use the term return to return a value in a function.
Note: Functions can only return value once. That means you can only the use the return statement once in your function.
Let's modify our function return a value.

Example - PHP Function returning a String value

<html> 
<body> 

<?php 

//we create a function named my_function 
function my_function($name) { 
 return "hello there $name!"; 
} 

//we call our function like this when we want to use it 
echo my_function("Matt"); 

?>

</body> 
</html> 
The above function returns our string value instead echoing it inside. We call the function and echo its value outside. The above code outputs "hello there Matt!"
Pretty neat 'eh. Let's look at more examples.

Example - PHP Function returning a Integer value

Let's create a function to return an integer value.
<html>
<body>

<?php
//we create a function named my_function
function my_function($price, $tax)
{
 $total_price = $price + $tax;
 return $total_price;
}

//we call our function like this when we want to use it
echo "The total price after tax: \$" . my_function(50, 5);

?>

</body> 
</html> 
In the above function we calculate the total price and return it. The code outputs The total price after tax: $55.

Summary

Hopefully now you can see how we use functions in our code. We can use functions to organize our code into little meaningful chunks and use them as we need them in our code.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Learning Programming Language - Skyblue - Powered by Blogger - Designed by Johanes Djogan -