Should a Function always Return Something?

Below is a Function that I just created which returns a Section’s Name…


	function getSectionName($dbc, $slug){

		// Build query.
		$q1 = "SELECT name
				FROM section
				WHERE slug=?";

		// Prepare statement.
		$stmt1 = mysqli_prepare($dbc, $q1);

		// Bind variable to query.
		mysqli_stmt_bind_param($stmt1, 's', $slug);

		// Execute query.
		mysqli_stmt_execute($stmt1);

		// Store results.
		mysqli_stmt_store_result($stmt1);

		// Check # of Records Returned.
		if (mysqli_stmt_num_rows($stmt1)==1){
			// Section Found.

			// Bind result-set to variables.
			mysqli_stmt_bind_result($stmt1, $sectionName);

			// Fetch record.
			mysqli_stmt_fetch($stmt1);

			// Return Section Name.
			return $sectionName;

		}else{
			// Section Not Found.
			$resultsCode = 'FUNCTION_SECTION_NOT_FOUND_5010';

			// Set Error Source.
			$errorPage = $_SERVER['SCRIPT_NAME'];

			// Log Function Error.
			logError($dbc, $resultsCode, $errorPage);

			// End script.
			exit();

		}//End of CHECK FOR SECTION

	}

I am wondering if I should return a NULL or ‘’ in the ELSE branch in addition to logging the Error? :-/

If I don’t do that, then in my calling script, I would have to check for a Null like this to avoid an error…


	<title><?php echo (isset ($sectionName) ? $sectionName : '') ?> Section</title>

…which seems silly.

Suggestions?

Thanks,

Debbie

You are using “exit” at that moment execution stops nothing matters after that.

I am testing my code, and realizing that I have a bigger issue…

What I want to happen is this…

In “index.php” I have this line in my php section which comes before my HTML section…


$sectionName = getSectionName($dbc, $slug);

And then a few lines later I have…


<!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>
	<!-- HTML Metadata -->
	<title><?php echo (isset ($sectionName) ? $sectionName : 'EMPTY') ?> Section</title>

If getSectionName() successfully finds the “Section Name” for the “Section Slug” in the URL, then I want it displayed in <title> and also below in my HTML code (not shown here).

If getSectionName() cannot find the “Section Name” for the “Section Slug” in the URL, then I want…

1.) To call this function so I can log the error…


		// Log Function Error.
		logError($dbc, $resultsCode, $errorPage);

2.) I want to return something like the Zero-Length String, so my HTML page still loads and doesn’t crash.

How can I have it both ways?! :-/

Thanks,

Debbie

Define section name at the top of your function:


    function getSectionName($dbc, $slug){
         $sectionName="";

        // if, (buts and maybes) {
        // do your stuff here
        // else chuck silent errors
        }

    // then no matter what happens:
    return $sectionName;
    }

You could develop that idea further if you wanted:


    function getSectionName($dbc, $slug, $default = ''){
         $sectionName= $default;

    // do your stuff here
    // else chuck silent errors


    // then no matter what:

    return $sectionName;
    }

This would leave you open to using it like:


echo sectionName($foo,$bar);
// OR adding something slightly more helpful
echo sectionName($foo,$bar, $siteName);

The cost is that you have an extra [optional] argument going into your function, some might find that ugly.

logic_earth & Cups,

The way I fixed it was simply removing exit() in my logError() function, so that after that function is called, control just continues in the parent code and then I can keep my return “” in the ELSE branch of my code.

Thanks,

Debbie