Table of Contents

PHP Functions

One

What is a function?

<?php

	$a = readLine("Your message: ");
	echo str_rot13($a) . "\n";

?>

Two

Let's make functions to do stuff!

<?php

	echo add(2,2) . "\n";
	echo sub(3,2) . "\n";

	function add($a, $b) { return $a + $b; }
	function sub($a, $b) { return $a - $b; }
	function mul($a, $b) { return $a * $b; }
	function div($a, $b) { return $a / $b; }

?>

Three

<?php

	$n = 1;

	while ($n <= 10) {

		if (is_even($n)) {
			echo "{$n} is even.\n";
		} else {
			echo "{$n} is odd.\n";
		}
		$n = $n + 1;
	}


function is_even($a) {
	return ($a % 2) == 0;
}