ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

PHP Sample Codes On Function: How to make Function in PHP

Updated on February 22, 2013

PHP code, php script, php example code on making a function.


How to make a function in PHP programming? Knowing how to make a function on php programming for php developer wannabes is very essential, because you can apply an object oriented programming through function. Object oriented programming is chopping your codes into block of codes that has unique functionality with each other. So that when there is a bug in your program you can easily pin point on where the bug is and what block of codes went wrong. Through the example code below, you will at least see how to make a function in PHP and learn to make your own too.



To learn more, if you still do not know how to run a php code on your browser please refer to the link below for some easy to follow steps.

How to Run a PHP Code in Your Own Browser Through XAAMP and Notepad++.

Also, if you do not know how to make an html page and how to run one in your browser, you might want to learn it, for html will be very essential on your learning towards php language. Below is the link for html basics.

HTML Basic: Making, Testing and Running a HTML File



Sample PHP Code on Function

<?php
	function sum($a, $b)
	{
		$sum = $a + $b;
		return $sum;
	}
	
	function subtract($a, $b)
	{
		$difference = $a - $b;
		return $difference;
	}
	
	function multiply($a, $b)
	{
		$product = $a * $b;
		return $product;
	}
	
	function divide($a, $b)
	{
		$divide = $a / $b;
		return $divide;
	}
	echo "<strong>Functions' Output</strong><br/><br/>";
	
	echo "The sum of 100 and 50 is: ". sum(100, 50)."<br />";
	echo "The product of 100 and 50 is: ". multiply(100, 50)."<br />";
	echo "The difference of 100 and 50 is: ". subtract(100, 50)."<br />";
	echo "The quotient of 100 and 50 is: ". divide(100, 50)."<br />";
?>

Sample Output for Function

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.