php
PHP Functions
PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are 1000 of built-in functions in PHP.

PHP User-Defined Functions

In addition to the built-in functions, PHP also allows you to define your own functions. It is a way to create reusable code packages that perform specific tasks and can be kept and maintained separately form main program. Here are some advantages of using functions :

Code Reusability :  PHP functions are defined only once and can be invoked many times, like in other programming languages.

Less Code : It saves a lot of code because you don't need to write the logic many times. By the use of function, you can write the logic only once and reuse it.

Easy to understand : PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.
Syntax :
function functionname(){ 
  //Some code to be executed 
}
The above syntax, declaration of a user-defined function start with the word function, followed by the name of the function you want to create followed by parentheses i.e. () and finally place your function's code between curly brackets {}.
Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Functions</title>
</head>

<body>

<?php
function ftl() {
    echo "Free Time Learning...!";
}

ftl(); //calling function
?>

</body>
</html>
Output :
PHP Function Arguments

We can pass the information in PHP function through arguments. An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example to pass single argument in PHP function.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Function Arguments</title>
</head>

<body>


<?php
	function Books($book_name) {
		echo "$book_name Tutorial.<br />";
	}
	
	Books("HTML5");
	Books("CSS3");
	Books("JavaScript");
	Books("jQuery");
	Books("Bootstrap");
	Books("PHP");
	Books("mySQL");
	Books("AngularJS");
?>

</body>
</html>
Output :
The PHP function with two arguments ($book_name and $year) :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Function Arguments</title>
</head>

<body>

<?php
function Books($book_name, $year) {
    echo "$book_name Tutorial. Released in $year <br>";
}

Books("HTML5","2014");
Books("CSS3","2001");
Books("JavaScript","1995");
Books("jQuery","2006");
Books("Bootstrap","2011");
Books("PHP","1994");
Books("MySQl","1995");
Books("AngularJS","2010");
?>

</body>
</html>
Output :
PHP Functions : Default Argument Value

We can specify a default argument value in function. While calling PHP function if you don't specify any argument, it will take the default argument. The following example of using default argument value in PHP function.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Default Argument Value</title>
</head>

<body>

<?php
	function my_website($website = "www.freetimelearn.com") {
		echo "Website name is : $website <br />";
	}
	
	my_website("www.freetimelearning.com");
	my_website(); // will use the default value
	my_website("www.freetimelearning.xyz");
	my_website("www.freetimelearn.xyz");
?>

</body>
</html>
Output :
PHP Functions : Returning Value

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code. The following example of PHP function returning a value :

<!DOCTYPE html>
<html>
<head>
	<title>PHP Function - Returning Value</title>
</head>

<body>

<?php
	 function ret_value($x, $y) {
		$sum = $x + $y;
		return $sum;
	 }
	 $return_value = ret_value(10, 20);
	 
	 echo "Returning value from the function : $return_value";
?>

</body>
</html>
Output :
PHP Dynamic Function Calls

It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.

The Following example of Dynamic function calls :

<!DOCTYPE html>
<html>
<head>
	<title>PHP Dynamic Function Calls</title>
</head>

<body>

	  <?php
         function ftl() {
            echo "www.freetimelearning.com <br />";
         }
         
         $FreeTimeLearn = "ftl";
         $FreeTimeLearn();
      ?>

</body>
</html>
Output :