PHP - Function

The function performs a specific task or block of code the main advantages of using functions is Reducing duplication of code, Improving clarity of the code, Reuse of code, Information hiding and more advantages

Syntax of Function


<?php
	function  (arguments)
	{
		//Code Here
	}
	<function name> //call function
?>							
							

Example of Function


<?php
	 function demo()
	 {
		echo "Hello This is Demo Function";
	 }
	 
	 demo(); //call function
?>						
							

php also support user define function the following list of user define function available in php that explain in details.

User Defined Functions

     1. No Arguments No Return Value  

     2. No Arguments With Return Value  

     3. With Arguments No Return Value  

     4. With Arguments With Return Value  


No Arguments No Return Value

In this function can not pass arguments and no return value it simple function that function call and display function data the following example of no arg and no return value function.


<?php
	function add()
	{
		$a=10;
		$b=20;
		echo $a+$b;
	}
	add();
?>						
							

No Arguments With Return Value

In this function not pass arguments but return value the following example of no arguments and with return value.


<?php
	 function sub()
	{
		$a=10;
		$b=20;
		return $a-$b;
	}
	echo sub();
?>				
							

With Arguments No Return Value

in this function can pass arguments but not return a value following example of with arguments and no return value.


<?php
	function mul($a,$b)
	{ 
		echo $a*$b;
	}
	$a=10;
	$b=20;
	mul($a,$b);
?>								
							

With Arguments With Return Value

In this function can pass arguments and return a value the following example of with arguments and return value.


<?php
	function mul($a,$b)
	{ 
		return ($a/$b);
	}
	$a=10;
	$b=20;
	echo div($a,$b);
?>					
							
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!