PHP User Defined Function Syntax & Example Code with Return Parameter

PHP User Defined Functions

PHP User Defined functions mean that a function which is created by a user. The declaration of the user-defined function will start with the “function” keyword.

Syntax of PHP user defined function deceleration

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		function functionName()
		{
			code to be execute;
		}
	?>
  </body>
</html>

Note: The name of the function must be started with a character and underscore not to be with number.

Example 1 – User Defined Functions Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		function info() // creating a function
		{
			echo "Welcome to the Technology World";
		}
		
		info(); // calling of function 
	?>
  </body>
</html>

In the above example we exaplain how you can create a function and how you cann use this function.

Here we create a function with the name “info()” and this function have some return value. And last we call this function. The below screenshot is the result of the above code.

PHP User Defined Function
Fig.1 – User Defined Function Example Screenshot.

PHP Function Parameters or Arguments

The argument is not another thing it is just like a variable. When you pass some parameter or value inside the braces this is called Function Parameters or Arguments.

Example 2 – User defined function example with Parameters or Arguments

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		function cal($x,$y) // creating a function
		{
			echo "The value of x is " . $x . " " . "& y is " . $y . "<br />";
		}
		
		cal(5,6);
		cal(87,6);
		cal(98,82);
		
	?>
  </body>
</html>

In the above example, we create a function is cal and have 2 arguments $x and $y and the function cal is used to print the value of $x and $y.

PHP Function Parameters Example
Fig.2 – PHP Function Parameter Example.

Caution:You can add many arguments as your requirement but you have to separate with a comma “,” symbol.

PHP Function Return a Value

PHP Function Return means that a function returns a value. And to return a value you have to use the “return” keyword.

Example 3 – User defined function example with return value

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		function sq($x) // creating a function
		{
			$z= $x * $x;
			return($z);
		}
		
		echo sq(65) . " is the square of 65" . "<br />";
		echo sq(74) . " is the square of 74" . "<br />";
		echo sq(87) . " is the square of 87" . "<br />";
		echo sq(73) . " is the square of 73" . "<br />";
		
	?>
  </body>
</html>

In the above example, we create a function sq() and this function calculates the square of a number. In this function, we write the calculation of a square.

PHP Function Return the calculated square value
Fig.3 – Function Return the square value to the calling program or main program.

PHP Factorial Function

Example 4 – PHP Factorial Function Code return the calculated factorial value

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		function fact($x) // creating a function
		{
			$fact1=1;
			for($i=1; $i<=$x; $i++)
			{
				$fact1 = $fact1 * $i;
			}
			return $fact1;
		}
		
		echo fact(5) . " is the factorial of 5" . "<br />";
		echo fact(6) . " is the factorial of 6" . "<br />";
		echo fact(7) . " is the factorial of 7" . "<br />";
		echo fact(13) . " is the factorial of 13" . "<br />";
		
	?>
  </body>
</html>

In the above example, we create a function fact() and this function calculates the factorial of a number($x).

PHP Factorial Function example
Fig.4 – PHP Factorial Function code output screenshot.
You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape