Jump to content

[SOLVED] Functions within Function question


dws90

Recommended Posts

First, some background: I'm making a PHP script for a school to automatically generate it's student directory (the list of all the students and their addresses that is handed out to the families). The script is functional, but there's a change I've been asked to make. If the parents are separated, it's supposed to print the address of whichever parent the student lives with first, instead of always putting the mother first like it currently does. Fine, I say, I'll just move those parts into displayMother() and displayFather() functions and switch the call order as needed (yes, those could probably be abstracted, but this isn't meant to be a beautiful program - it's a hack for internal use).

 

The meat of my question is: having the displayMother() and displayFather() functions floating around in the main body of the program seems messy to be. I'd much rather declare them within the existing displayEntry() function right before they're used. According to the PHP manual, it's possible to have functions declared within functions a la Lisp, which is exactly what I'm looking for. Here's the relevant part of what I have:

else {
	/* Put these in functions to be more easily manipulated */
	function displayMother($entry) {
		/* Mother */
		if($entry[MOTHER_LASTNAME] != "")
		{
			print $entry[MOTHER_PREFERREDNAME]." ".$entry[MOTHER_LASTNAME]."<br />";
			displayln($entry[MOTHER_ADDRESS]);
			displayln($entry[MOTHER_CITY]." ".$entry[MOTHER_ZIP]); 
			displayln($entry[MOTHER_HOME_PHONE]);
		}
	}
	function displayFather($entry) {
		/* Father */
		if($entry[FATHER_LASTNAME] != "")
		{
			print $entry[FATHER_PREFERREDNAME]." ".$entry[FATHER_LASTNAME]."<br />";
			displayAddress(	$entry[FATHER_ADDRESS], $entry[FATHER_CITY], 
				$entry[FATHER_STATE], $entry[FATHER_ZIP], 
				$entry[FATHER_HOME_PHONE]);
		}
	} 
	if($entry[sTUDENT_ADDRESS] == $entry[FATHER_ADDRESS]) {
		displayFather($entry);
		displayMother($entry);
	}
	else {
		displayMother($entry);
		displayFather($entry);
	}
}

 

When I run that, however, I get the following error:

Fatal error: Cannot redeclare displaymother() (previously declared in /Applications/xampp/xamppfiles/htdocs/facebook/facebookparser.php:123) in /Applications/xampp/xamppfiles/htdocs/facebook/facebookparser.php on line 123

 

Since the function containing this, displayEntry(), is being called multiple times, it looks like the functions are being declared multiple times. Is there a way that I can declare displayMother() and displayFather() that would allow me to leave them as internal functions but only have them declared when displayEntry() is first run?

if (!function_exists('displayMother')) {

  function displayMother($entry) {

...

}

}

 

But I don't know why you don't just create the functions elsewhere and use them inside the displayEntry() function.  I think it's kind of stupid actually.  Sorry to be so blunt, but I just don't see the point. =/  >_>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.