PHP Functions

PHP functions are used to reuse piece of code many times.  There are various built-in functions in PHP. A function will be executed by a call to the function. Following are the advantage of PHP function.

  • Code Reusability
  • Less Code
  • Easy to understand
PHP User define Functions PHP user-define functions are used to create own functions.  Let’s see how to declare user define function. Syntax functionfunctionName(){ //code to be executed; } Note: Function name must be start with a letter or underscore (not a number). Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
                functionabc(){
                                echo "Created User define function !";
                }
                abc(); // calling a function
                ?>
</body>
</html>
Output Created User define function ! PHP function with Arguments In PHP function, we can pass the information through arguments by comma. It supports Call by value, Call by Reference, Default argument values and variable –length argument list.  Example 1
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
            function name($nm){
                        echo "Your Name is:"."$nm"."<br>";
            }
            name('Alok'); // calling function With  single argument
            name('John'); // calling function with single argument
            ?>
</body>
</html>
Output Alok John Example2
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
            function name($nm,$age){
                        echo "Your name:"."$nm"."  "."Age"."$age"."<br>";
            }
            name("Alok","20"); // calling function With  two argument
            name('John',"30"); // calling function with two argument
            ?>
</body>
</html>
Output Your name:Alok Age20 Your name:John Age30 PHP Call By Reference In PHPcall by reference, the function does not modify the actual value by default. But if we want to pass the value as a reference then it is possiblethrough ampersand ( &) symbol. Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
            function adder(&$val)
            {
                $val .= 'Call By Reference';
            }
            $nm= 'Hello ';
            adder($nm);
            echo $nm;
?>
</body>
</html>
Output Hello Call By Reference PHP Call By Value Call by value is used to pass the value directly to a function.  The called function uses the value in a local variable.  There is no affect the source variable when change it. Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
            <?php
 //Call by value program
            functionabc($x){
            $x=$x+30;
            return($x);
            }
 $a=40;
echoabc($a)."<br>";
echo ($a);
 ?>
</body>
</html>
Output 70 40 PHP Default Argument Value Function Sometime we do not want to pass any value to the function then it will use default arguments in function. Example
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
            <?php
            function fun($name="Ram"){
            echo "Hello $name<br/>";
            }
            fun("John");
            fun();//passing no value
            fun("Rohan");
            ?>
</body>
</html>

Output Hello John Hello Ram Hello Rohan