A PHP function contains block of codes that we can execute repeatedly at any moment by calling the function’s name.

Creating a function is easy although it sounds very advance for beginners but I’m telling you, once you learn how to create your own function, you will love doing it over and over in your program.

PHP has more than 1000 pre-defined functions that you can use in your script. But sometimes, these functions are not that helpful to fill our needs. In that case, we need to create our own functions, and that’s what we call user-defined functions.

Creating a Function

A user-defined function declaration start with a word function.

  • A function is a block of code that we can use repeatedly in an application.
  • A function will not execute directly on page load.
  • A function will execute by calling the function.
  • A Function name is case sensitive.
  • A Function name can start with letter or underscore (not number).
  • A Function should contain open and close “()” parenthesis after the name.
  • A Function must end with open and close bracket “{}” where the code to be executed is placed.

Syntax:
function function_name(){
code to execute
}

Example


<?php
function my_name(){
echo "My name is Bryan";
}
?>

Calling the Function

To call the function, just write its name

Example


<?php
function my_name(){
echo "My name is Bryan";
}

//calling the function
my_name(); //print My name is Bryan
?>

Function with Arguments

Information can be used inside the function by passing it through arguments.

  • Arguments are specified after the function name, inside the parentheses.
  • You can add many arguments by separating them with comma (,).

Example


<?php
//function with single argument
function my_name($name){
echo "My name is $name";
}

//function with multiple arguments
function full_name($firstname, $lastname){
echo "My full name is $firstname $lastname";
}

//calling the function
my_name("Bryan"); //print My name is Bryan

full_name("Bryan", "Lusica"); //print My full name is Bryan Lusica
?>

Arguments with default value

An argument can have a default value. If no information is passed with the function, the default value will be used.

Example


<?php
function my_name($name='Bryan'){
echo "My name is $name";
}

//calling the function
my_name("John"); //print My name is John
my_name("Petter"); //print My name is Petter
my_name(); //print My name is Bryan
?>

Function with Returning Value

To let the function return a value, you can simply use return statement.

Example


<?php
function my_name($name){
return $name;
}

//using the function's return value
echo my_name("John"); //print John
print my_name("Petter"); //print Petter
$fname = my_name("Bryan"); //$fname has a value Bryan
?>

How to take Cialis it seems to me that would already be time to cease to discuss this question. As I already answered it not once. Have it in a look. You shouldn’t worry! All deer go south when they are called by homesickness. Are turned and lie on snow till some hours.