PHP excels in the scope and breadth of built-in functions it provides: There are over 700 core functions, and several hundred more are provided by the many built-in [[PHP extensions]]. Whenever you have a data processing task, be sure to search the [http://www.php.net/manual/en/ ”’PHP manual”’] first, before you recreate the wheel. Learning to use the built-in functions is one of the main differentiators between a seasoned programmer and a beginner.

= What is a function? =

The word ”function” comes from the word ”functionality”; at its heart, a function is a collection of code that does something.

There are two major types of functions: * ”’Built-in functions”’ — These are what are considered the “core language” functions and are those found in the [http://www.php.net/manual/en/ ”’PHP manual”’]. * ”’User-defined functions”’ — These are functions that are created inside PHP applications and libraries. These include all of the functions you create.

= How do I create a user-defined function? =

Functions have the syntax of function functionName($inputParameter) { // Do stuff here.

// Optionally, we can return a value, which also ends the function:
return true;

}

= How do I execute a function? =

You’ll need to figure out the function’s name and what its input parameter(s) should be, if anything. For instance, the [http://us.php.net/manual/en/function.print.php ”print” function] needs a string to ouput:

print(‘Hello, World!’); // Output: Hello, World! [http://www.phpexperts.pro/book/function_fun/hello_world.php View the output.]

= Simple Function =

Let’s write a function to calculate the miles per gallon we get in a car. Our inputs will be ”$milesDriven” and ”$tankSize”.

function calculateMilesPerGallon($milesDriven, $gallonsPerTank) { // First, let’s calculate the raw MPG: $mpg = $milesDriven / $gallonsPerTank;

// Now, let's round it to the first decimal place using PHP's [http://us.php.net/manual/en/function.round.php '''built-in round() function''']:
$rounded = round($mpg, 1);

return $rounded;

}

// Let’s calculate the MPG of a 2010 Honda Insight Hybrid: print(calculateMilesPerGallon(515, 10.6) . ” miles per gallon”); // Output: 48.6 miles per gallon [http://www.phpexperts.pro/book/function_fun/mpg.php View the output.]

= Optional Input Parameters = There will be times when giving a function an optional input parameter or two makes sense. There are two rules that must be obeyed: * Optional input parameters are assigned a value in the function declaration, * Optional input parameters must come at the end of the list of parameters.

function say($words, $who = ‘He’, $volume = ‘softly’) { print “$who said, "$words", $volume.\n”; }

say(‘Hi’); say(‘Hello’, ‘She’); say(‘Good bye’, ‘They’, ‘loudly’); // Output: He said, “Hi”, softly. // She said, “Hello”, softly. // They said, “Good bye”, loudly. [http://www.phpexperts.pro/book/function_fun/he_said_she_said.php View the output.]

= PHP Tutor Lessons = * [http://test.phpexperts.pro/tutor.php?lessonID=2 Lesson 2: Functions]

= Exercise = The area of a triangle is determined by the equation Area = (Base * Height) / 2. Create a function called ”getTriangleArea” that returns the area of a triangle. Be sure to round the results to the nearest decimal place.

To test your results, inputs of 3 and 5 should equal 7.5, 3.5 and 5 should equal 8.8.