| Home | Syllabus | Lectures | Assignments | Project |
function /* Function name */ ( /* Arguments of function, separated by commas */ ) { /* Code for function goes here */ }
| Simple PHP Functions (samp14a.php) |
|---|
<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 14A</title> <link rel="stylesheet" type="text/css" href="../styles.css" /> </head> <body> <p> <?php // Writing a simple function // Sum two parameters and return the total function sum($a, $b) { $c = $a + $b; return $c; } // Sum an entire array of values (a list) function sum_array($sum_arr) { $total = 0; foreach ($sum_arr as $x) $total += $x; return $total; } // Print the contents of an array of values (a list) function print_array($arr) { print( "Array: " ); foreach ($arr as $x) print( "$x " ); print( "<br />\n" ); } $x = 3; $y = 4; $my_array = array(1, 2, 3, 4, 5); // Calling functions with variables $result = sum($x, $y); print( "Sum: $x+$y=$result<br />\n" ); $arr_result = sum_array($my_array); print_array($my_array); print( "Array Sum: $arr_result<br />\n" ); // Calling functions with variables and/or values $result = sum($x, 10); print( "Sum: $x+10=$result<br />\n" ); $arr_result = sum_array(array(2,4,6,8)); print_array(array(2,4,6,8)); print( "Array Sum: $arr_result<br />\n" ); ?> </p> <!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html> |
| Globals and Functions in PHP (samp14b.php) |
|---|
<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 14B</title> <link rel="stylesheet" type="text/css" href="../styles.css" /> </head> <body> <p> <?php // Declare some global variables $g1 = 4; $g2 = 3; // Sum and return the total of $a, $b, $g1, and $g2 // Notice that only $a and $b are summed, since // integers default to 0 if they don't exist function sum($a, $b) { $c = $a + $b + $g1 + $g2; print( "inner c: $c<br />\n" ); return $c; } // Sum and return the total of $a, $b, $g1, and $g2 // This time use the global keyword function sum_dwg($a, $b) { global $g1, $g2; $c = $a + $b + $g1 + $g2; print( "inner c: $c<br />\n" ); return $c; } // Sum and return the total of $a, $b, $g1, and $g2 // This time use the global keyword // Also declare the helper $c to be global, so it will // stick around after the function is done function sum_special($a, $b) { global $g1, $g2, $c; $c = $a + $b + $g1 + $g2; print( "inner c: $c<br />\n" ); return $c; } // Print each member of the superglobal $GLOBALS function print_sg() { print( "All Global Variables:<br />\n" ); foreach ($GLOBALS as $id => $value) print( "$id => $value<br />\n" ); } print( "Global Vars:<br />\ng1: $g1<br />\ng2: $g2<br /><br />\n" ); $x = 11; $y = 20; // Calling functions without explicit global $result = sum($x, $y); // c is a local variable to sum(), so it has fallen out of scope // so this should print nothing print( "outer c = $c<br />\n" ); print( "sum($x,$y) = $result<br /><br />\n" ); // Calling functions with explicit global $result = sum_dwg($x, $y); // c is a local variable to sum_dwg(), so it has fallen out of scope // so this should print nothing print( "outer c = $c<br />\n" ); print( "sum_dwg($x,$y) = $result<br /><br />\n" ); // Calling functions with explicit global and global c $result = sum_special($x, $y); // c is specified global to sum_special(), so it is in scope print( "outer c = $c<br />\n" ); print( "sum_special($x,$y) = $result<br /><br />\n" ); // Print the superglobal $GLOBALS print_sg(); ?> </p> <!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html> |
| PHP Static Variables (samp14c.php) |
|---|
<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 14C</title> <link rel="stylesheet" type="text/css" href="../styles.css" /> </head> <body> <p> <?php // Make a simple function that increments a local variable function simple() { $a = 0; $a++; print( "a: $a<br />\n" ); } // Make a simple function that increments a static local variable function simple_use_static() { static $a = 0; $a++; print( "a: $a<br />\n" ); } // Call the simple function 5 times for ($i = 0; $i < 5; $i++) simple(); print( "<br />\n" ); // Call the simple_use_static function 5 times for ($i = 0; $i < 5; $i++) simple_use_static(); ?> </p> <!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html> |
| PHP Pass by Reference (samp14d.php) |
|---|
<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 14D</title> <link rel="stylesheet" type="text/css" href="../styles.css" /> </head> <body> <p> <?php // Simple pass by value function function pbv($x) { $x++; print( "x in function: $x<br />\n" ); } // Simple pass by reference function function pbr(&$x) { $x++; print( "x in function: $x<br />\n" ); } // Simple array pass by value function function arr_pbv($arr) { $arr[0]++; print( "arr[0] in function: $arr[0]<br />\n" ); } // Simple array pass by reference function function arr_pbr(&$arr) { $arr[0]++; print( "arr[0] in function: $arr[0]<br />\n" ); } $x = 3; // Demonstrating pass by value print( "x before function: $x<br />\n" ); pbv($x); print( "x outside function: $x<br />\n" ); print( "<br />\n" ); // Demonstrating pass by reference print( "x before function: $x<br />\n" ); pbr($x); print( "x outside function: $x<br />\n" ); print( "<br /><br />\n" ); $arr = array(4, 5, 6); // Demonstrating pass by value print( "arr[0] before function: $arr[0]<br />\n" ); arr_pbv($arr); print( "arr[0] outside function: $arr[0]<br />\n" ); print( "<br />\n" ); // Demonstrating pass by reference print( "arr[0] before function: $arr[0]<br />\n" ); arr_pbr($arr); print( "arr[0] outside function: $arr[0]<br />\n" ); ?> </p> <!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html> |