PHP: User-Defined Functions


Example: Simple PHP User-Defined Functions

In this function, no data is being passed in and no data is being passed back out to the main part of the program.


	<?php
	function outHead() {
		echo "<h1>Welcome</h1>";
		echo "<p>This web site contains programming tutorials.</p>";
	}

	outHead(); // call the function
	?>
	

Run it now


Example: Simple PHP User-Defined Functions

In this function, the variable $f is being passed into the function, but no data is being passed back out into the main part of the program.


	<?php
	function convertF2C($f) 
	{
		$c = round ( ($f - 32) * (5 / 9), 2);
		echo "<p>$f F is equal to $c C.</p>";
	}

	$f=33;
	convertF2C($f); 
	?>
	

Run it now


Example: Simple PHP User-Defined Functions

In this function, the variable $f is being passed into the function and the variable $c is being passed back out into the main part of the program.


	function convertF2C($f) 
	{
		$c = round ( ($f - 32) * (5 / 9), 2);
		return $c;
	}

	$f=33;
	$c = convertF2C($f); 
	echo "<p>$f F is equal to $c C.</p>";
	?>
	

Run it now