Creating user defined php functions

Creating user defined php functions, this is not about built-in PHP functions, this is about creating user defined custom functions. Function is a block of statements that we can reuse them in other places. Function’s will not executed immediately on page load. These functions will be executed after calling them. We can use these functions repeatedly throughout our code as many number of times as possible.

1

Syntax of user defined php functions

Function will be declared by the word “function”, followed by function name. After that braces, inside these braces arguments can be passed. Then after that curly braces inside curly braces our code will present. For calling this function, call it by function name then braces and a semicolon.

Syntax of user defined php functions with code snippet

<?php
function MyMsg() {
  echo "Hello world!";
}
?>

Next thing calling function, use function name with braces.

<?php
function MyMsg() {
  echo "Hello world!";
}
MyMsg();
?>
2

PHP functions with arguments

Arguments can be used to pass information into PHP functions. An argument is like an variable. Arguments are specified after the function name, inside the brackets. Arguments can be used any number of times, separate these multiple arguments with commas.

In this simple function, I’m using one argument called first name, and also i can use second argument called last name. First we will see an example code snippet for one argument function.

3

Example code snippet for user defined php functions with one argument

<?php
function MyMsg($fname) {
  echo "Hello $fname!";
}
MyMsg("vivek"); //output will be "Hello vivek!"
?>

Here the output will be “Hello vivek!”, in that brackets you can use any name.

4

Example code snippet for user defined php functions with two argument

Here these two arguments are one is first name and the other one last name, I’ll pass these two arguments into this function.

<?php
function MyMsg($fname, $lname) {
  echo "Hello $fname $lname!";
}
MyMsg("vivek", "kumar"); //output will be "Hello vivek kumar!"
?>

And the final output is “Hello vivek kumar!”.

5

User defined PHP functions with default argument value.

Instead of passing two arguments inside function, we can pass one of these arguments as default values. You can overwrite this default value by specifying some values in function itself.

<?php
function MyMsg($fname, $lname='kumar') {
  echo "Hello $fname $lname!";
}
MyMsg("vivek", "kumar"); //output will be "Hello vivek kumar!"
?>
5

User defined PHP functions with returning values.

While using PHP functions, we don’t print it out directly. We will assign that value to a variable that will be returned out of the function. while calling function, returned value will be assigned to a variable and print that out.

<?php
function MyMsg($fname, $lname='kumar') {
  $val "Hello $fname $lname!";
  return $val;
}
$op = MyMsg("vivek", "kumar"); //output will be "Hello vivek kumar!"
echo $op;
?>

If you have any problem while working with functions, let me know using comment form below.

Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe