PHP user-defined functions (part 2)

An exercise on functions


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
List formatting
</title>
<?php 
$fruit = array("apple", "banana", "orange", "grape", "kiwi"); 
$seasons = array("winter", "spring", "summer", "fall");

// write a function print_list to print
// the array as an ordered list if the boolean is 'true',
// as an unordered list otherwise

?>
</head>
<body>
<h2>Printing out two lists</h2>
<?php
// print an unordered list of fruit
print_list($fruit, false);
// print an ordered list of seasons
print_list($seasons);
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring08/functions/list.php

Using a function to switch two strings - first attempt


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Table formatting
</title>
<?php 
$string1 = "A shorter string";
$string2 = "A very long string that goes on for a while and then some 
until it finally ends at some point long in the future";

// A function that switches the two strings directly in the 
// calling program so that the longer one is first. 
// the strings are passed by reference
function sort_strings($str1, $str2) {
	print "<p>Beginning of the function: $str1, $str2 </p>\n";
	$temp = $str1;
	$str1 = $str2;
	$str2 = $temp;
	print "<p>End of the function: $str1, $str2 </p>\n";
}

?>
</head>
<body>
<?php
sort_strings($string1, $string2);
?>
<table border="1">
<tr>
<?php
print "<td style=\"width: 70%\">$string1 </td><td> $string2 </td>\n";
?>
</tr>
</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring08/functions/string_table_not_working.php

Using a function to switch two strings - pass by reference


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Table formatting
</title>
<?php 
$string1 = "A shorter string";
$string2 = "A very long string that goes on for a while and then some 
until it finally ends at some point long in the future";

// A function that switches the two strings directly in the 
// calling program so that the longer one is first. 
// the strings are passed by reference
function sort_strings(&$str1, &$str2) {
	$temp = $str1;
	$str1 = $str2;
	$str2 = $temp;
}

?>
</head>
<body>
<table border="1">
<tr>
<?php
sort_strings($string1, $string2);
print "<td style=\"width: 70%\">$string1 </td><td> $string2 </td>\n";
?>
</tr>
</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring08/functions/string_table_reference.php

Switching elements of an array - pass by reference


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/13/08
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Table formatting
</title>
<?php 
$strings = array("A shorter string", 
"A very long string that goes on for a while and then some until it 
finally ends at some point long in the future"); 

// a php function 

// the function takes an array of two strings switches 
// them if needed so that the longer one is first. 

// the array is passed by reference (the & in the function header)

function sort_strings(&$str_array) {
	if (strlen($str_array[0]) < strlen($str_array[1])) {
		$temp = $str_array[0];
		$str_array[0] = $str_array[1];
		$str_array[1] = $temp;
	}
}
?>
</head>
<body>
<table border="1">
<tr>
<?php
sort_strings($strings);
print "<td style=\"width: 70%\"> $strings[0] </td><td> $strings[1] </td>\n";
?>
</tr>
</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring08/functions/string_table_array.php

UMM CSci 1101

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.