PHP Function

PHP Function

Function syntax

function function_name() {
php code;
}

Function example

<html>
<body>
<?php
	function Test() {
		echo "I like PHP functions!";
	}

	Test(); 
?>
</body>
</html>

Result

I like PHP functions!

Function with parameters

<html>
<body>
<?php
	function pFunction($p_name, $p_price) {
		echo "The $p_name price is: $p_price <br />";
	}

	pFunction("PHP", "23");
	pFunction("HTML", "18");
?>
</body>
</html>

Result

The PHP price is: 23
The HTML price is: 18

Function with return value

<html>
<body>
<?php
	function pSum($a, $b) {
		$c = $a + $b;
		return $c;
	}

	echo "The value return value: ", pSum(2, 3);
	echo "<br />";	
	echo "The value return value: ", pSum(7, 5);
?>
</body>
</html>

Result

The value return value: 5
The value return value: 12