Function and its type in PHP | User-defined function in PHP



Agenda:

  • Define a function in PHP
  • How to define a user-defined function? explain with example in PHP
  • PHP function with parameters
  • PHP functions returning value
  • Variable function in PHP
  • Lambda or anonymous function in PHP



-PHP functions are similar to other programming languages. A function is a set of instruction which take one more input in the form of parameter and does some processing and return a value. or function is a piece of code.
-There are so many built-in functions in PHP but PHO gives you the option to create your own functions as well. A function will be executed by a call to function. You may call a function from anywhere from within the program.

-User-defined function
  • Creating a PHP function
  • Calling a PHP function
-A user-defined function declaration starts with the word function.
- suppose we want to create a function to print hello (See below example) using function_name() you can call the PHP function.
Syntax:
function function_name()
{
 #code
}
Example:<!DOCTYPE html>
<html>
<head>
 <title>cwipedia.in| PHP User-defined Functions</title>
</head>
<body>
<?php
function Shivam()
{
 echo "Hello";
}

Shivam();
?>
</body>
</html>
output:
Hello



PHP functions with parameters:

-PHP also gives you to pass parameters inside the function. We can pass as many as parameters we like. The following example takes two integer parameters and add them together and then print them.
Example:<!DOCTYPE html>
<html>
<head>
 <title>cwipedia.in| PHP User-defined Functions</title>
</head>
<body>
<?php
function Add($a,$b)
{
 $c=$a+$b;
 echo "Addition:",$c;
}

Add(1,2);
?>
</body>
</html>
Output:
Addition:3



PHP Function returning value:

-A function can return a value using a return statement in conjunction with a value or object.retunr stop the execution of the function and sends the value to the calling code
-We can return more than one value from a function using return array(1,2,3)
Example:<?php
function Add($a,$b)
{
 $c=$a+$b;
 return print($c);
}

Add(1,2);
?>
Output:
3


Variable Function in PHP

-PHP supports the concept of variable functions. This means that if a variable name has parenthesis appended to it. PHP will look for a function with the same name as whatever the variable evaluates to and will attempt to execute it.
-Variable functions won't work with languages construct such as echo, print, unset(), isset(), empty(), include, require, and the like. Utile wrapper functions to make use of any of these constructs as a variable function.
Example:<?php

function s()
{
 echo "string";
}
$ss='s';
$ss();

$hi=function()
{
 echo "Hi";
};
if(is_callable($hi))
{
 echo "<br>yes";
}
?>
Output:
String
Yes

Anonymouse or Lambda function in PHP

  There are times when we need to create a small localized throw-away function consisting of few lines for a specific purpose; such as callback. It is unwise to pollute the global namespace with this kind of single-use functions. For such an event we can create an anonymous function using create_function(). The anonymous function allows the creation of function which has no specified name.
Example:<?php
$s='Shivam Dev';
$lambda=create_function('$n1', 'return "Coder";');
$r=preg_replace_callback('/world/',$lambda,$s);
echo $r;
?>
Output:
Shivam Coder


Comments