Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1."— Presentation transcript:

1 Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com © Copyright 2010, All Rights Reserved 1

2 Chapter Five PHP function, what’s your function? http://webcert.kirkwood.edu/~fmcclurg/c ourses/php/slides/chapter05.functions.ppt 2

3 Definition: A function is a reusable, self- contained (encapsulated) block of code with a well defined input and output. They are black box “number machines” that hide the internal implementation details. The function of a function 3

4 Similar to your TV remote, the user interface details are exposed (via push buttons), documented (via user’s manual) and the end result of using that interface is well defined (e.g. volume is increased, channel is incremented, etc.). The specific implementation details regarding how it actually works is masked from the user. Knowledge of internal operation of the remote control is not necessary for proper operation. Anatomy of a Function 0 11 inputinput outputoutput functionfunction 4

5 Description: There are hundreds of base functions that come bundled with PHP Examples: phpinfo(); echo("Open the pod bay doors, HAL."); $encrypted = md5( $password ); $unpad = trim( " padded string " ); $noCr = chop( "carriage return\n" ); $squareRoot = sqrt( 9 ); $timeStamp = time(); Base Functions 5

6 Description: In addition to using base functions, you can define your own custom functions. Function Definition Syntax: function functName( $param1,... ) {... return( $returnValue ); } Function Call Syntax: $returnVal = functName( $p1,... ); User Defined Function Syntax 6

7 <?php function isLeapYear( $year ) { if (((($year % 4) == 0) && (($year % 100) != 0)) || (($year % 400) == 0)) $result = TRUE; else $result = FALSE; return( $result ); } $yr = 2000; printf( "Year %d ", $yr ); $status = isLeapYear( $yr ) ? "is" : "is not"; printf( "%s a leap year", $status ); ?> User Defined Function Example 7

8 Problem: Write a function that converts Fahrenheit to Celsius. Requirements: 1. Write a driver that calls the conversion function with a single value. 2. Write a user-defined function that uses the following conversion formula: 3. Display the Fahrenheit value input and the calculated Celsius value returned from the function. Function Parameter: Variable for Fahrenheit. Function Return: Variable for degrees Celsius. Student Exercise (F to C Conversion) 8

9 <?php function f2c( $degreeF ) { $degreeC = ( 5 / 9 ) * ( $degreeF - 32 ); return( $degreeC ); } $fahrenheit = 212; $celsius = f2c( $fahrenheit ); printf( "%.1f Fahrenheit equals %.1f Celsius", $fahrenheit, $celsius ); ?> Student Solution (F to C Conversion) 9

10 Description: The default parameter initializes a function argument to a certain value if the parameter is omitted. Example: <?php function myLog( $exp, $base = 10 ) { return( log( $exp, $base ) ); } $result = myLog( 1000 ); printf( "log10(1000): %.0f ", $result ); $result = myLog( 8, 2 ); printf( "log2(8): %d ", $result ); ?> Functions with Default Parameters 10

11 Description: A static variable is not destroyed after the function exits. A static variable retains its value between function calls and stays in scope until the browser is closed. <?php function birthday() { // initialize once static $age = 0; return( ++$age ); // would this result in the same value? // return( $age++ ); } for ( $i = 0 ; $i < 5 ; $i++ ) printf( "Age: %d ", birthday() ); ?> Static Variables 11

12 to be continued... http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter05b.valueVsRef.ppt


Download ppt "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1."

Similar presentations


Ads by Google