Example: A Php program to show output using a user-defined function[UDF](No Argument-No Return).
<!DOCTYPE html>
<html>
	<body>

		<?php
			function msg()  //function created
			{
			   echo "Hello India world, how are you";
			}
			msg();  //function calling
		?>

	</body>
</html>
Example: A Php program to show output using a user-defined function(With Argument-No Return).
<!DOCTYPE html>
<html>
	<body>

		<?php
			function msg($country)  //parameterised function created
			{
			   echo $country;
			}
			msg("India");  //function calling
		?>

	</body>
</html>
Example: A Php program to show output using a user-defined function(No Argument-with Return).
<!DOCTYPE html>
<html>
	<body>
		<?php
			function msg()  //function created
			{
				$x="Great India";
				return $x;
			}
			echo msg();  //function calling
		?>
	</body>
</html>
Example: A Php program to show output using a user-defined function(with value as Argument-with Return).
<!DOCTYPE html>
<html>
	<body>
		<?php
			function msg($x,$y)  //argument & return function created
			{
			   $z=$x+$y;
			   return $z;
			}
			echo msg(10,20);  //function calling
		?>
	</body>
</html>

---------------------  OR  ----------------------

<!DOCTYPE html>
<html>
	<body>
           <?php declare(strict_types=1);            // strict is compulsory
               function addition(float $x, float $y) : int{
                   return (int)($x + $y);
                 }
                 echo addition(20.20, 25.30);
           ?>
        </body>
</html>          

Output:45
Example: A Php program to show default parameters using a user-defined function.
<!DOCTYPE html>
<html>
  <body>
    <?php

      // function with default parameter value 75
      function def($str, $num=75)
        {
	  echo "$str is $num years old";
        }
               
      def("John", 10);
      echo "</br>";

      // In this call, the default value 10
      def("Robert");
    ?>
  </body>
</html>

Output:
John is 10 years old
Robert is 75 years old   
Example: A Php program to show output using a user-defined function (with reference as Argument-with Return).
<!DOCTYPE html>
<html>
	<body>
		<?php
			function msg(&$num)  //function created
			{
				$z=$num+10;
				return $z;
			}

			$val=15;
			echo msg($val);  //function calling
			echo "<br>";
			echo $val;
		?>
	</body>
</html>
Output:
25
15

Loading

Categories: Php Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.