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

Related Topics

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP substr() Function

PHP substr() Function The substr() function in PHP returns the portion of the string specified by the start and length parameters. Syntax substr ( string $string , int $start [, int $length ] )  Parameter string (required)- This parameter represents the input string. It must be one character or longer. start (required)- This parameter specifies where to start...

2 minutes read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

1 minute read.

PHP stripos() Function

PHP stripos() Function The stripos() function in PHP finds the position of the first occurrence of a substring in the specified string. It is a case-insensitive a binary-safe function. The related functions are as follows: strripos() Functionstrpos() Functionstrrpos() Function Syntax stripos ( string $haystack , mixed $needle [, int $offset...

1 minute read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.

PHP next() Function

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

1 minute read.

PHP addslashes() Function

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP array_product() Function

PHP array_product() Function The array_product() function in PHP calculates the product of values in an array. Syntax array_product ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the product of the array elements as an integer or float....

1 minute read.

PHP stripslashes() Function

PHP stripslashes() Function The stripslashes() Function in PHP removes backslashes or un-quotes a quoted string. Syntax stripslashes ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with backslashes stripped off and the double backslashes (\\) are made...

1 minute read.

PHP substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP Validation

PHP validation is used to check whether the field is filled or not in the proper way by the user. There are two types of validation in PHP. Client Side Validation:...

5 minutes read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.