SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,914
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Should a Function always Return Something?

    Below is a Function that I just created which returns a Section's Name...

    PHP Code:
        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...

    PHP Code:
        <title><?php echo (isset ($sectionName) ? $sectionName ''?> Section</title>
    ...which seems silly.

    Suggestions?

    Thanks,


    Debbie

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You are using "exit" at that moment execution stops nothing matters after that.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,914
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    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...

    PHP Code:
    $sectionName getSectionName($dbc$slug); 
    And then a few lines later I have...
    HTML Code:
    <!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...
    PHP Code:
            // 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

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Define section name at the top of your function:

    PHP Code:
        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:

    PHP Code:
        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:
    PHP Code:
    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.

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,914
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •