user2691986
user2691986

Reputation:

Using a function as the parameter for another function in PHP

This may be strange and probably doesn't work but I wanted to check before I gave up on the idea. Essentially, I'm trying to use the results of a function as one of the parameters for another function. For example - function1($param1, function2($param1));.

The code I've included outputs everything I want it to, the only issue I'm having is that it outputs it in the wrong order - rather than outputting: logos, h2, form, h2, form; it outputs: form, h2, logos, form, h2.

// Display list
function ul($c, $p){
        echo '<ul class="'.$c.'">'.$p.'</ul>';
    }
    // Display list item
    function li($c, $p){
        echo '<li class="'.$c.'">'.$p.'</li>';
    }    
// Display navigation
        function nav(){
            require 'strings.php';
            // If the user is not logged in, display the login form
            if(!isset($_SESSION[$uss])){
                $nav = li('', require_once 'log_in.php');
            // Otherwise provide the user information and navigation
            }else{
                $nav = li($lks, $_SESSION[$uss][$uns]).
                       li($dss, $uss.' Id: '.$_SESSION[$uss]['id']).
                       li($dss, $uss.' Type: '.$_SESSION[$uss]['type']).
                       '<div id="display_projects_container">'.
                       require_once('display_projects.php').
                       '</div>'.
                       li($lks, '<a href="newsfeed.php" class="'.$lks.'">News Feed</a>').
                       li($dss, 'Check for updates.').
                       li($lks, '<a href="task_list.php" class="'.$lks.'">Task List</a>').
                       li($dss, 'Tasks to be completed.').
                       li($lks, '<a href="logout.php" class="'.$lks.'">Log Out</a>').
                       li($dss, 'Time for a burrito!');
            }
            echo '<div id="left_column">
                <!--After Breakpoint Logo-->
                <img src="logo.png" class="after_breakpoint_logo" />';
                ul('navigation', '<!--Before Breakpoint Logo-->'.
                                 li('before_breakpoint_logo', '<a href="index.php">
       <img src="logo.png" class="before_breakpoint_logo" /></a>').
                                 $nav);
                echo '</div><div id="content_container"><div id="content">';
        }

Is there a work around for this? Or is it just not possible to do?

Upvotes: 0

Views: 71

Answers (1)

lc.
lc.

Reputation: 116538

The problem is that you are echoing your results to the output. Instead you should use your li and ul functions to build up and return a string that the caller will then echo out or do as it pleases. Therefore your uland li functions should become:

function ul($c, $p){
    return '<ul class="'.$c.'">'.$p.'</ul>';
}

function li($c, $p){
    return '<li class="'.$c.'">'.$p.'</li>';
}    

You would then call li many times as you are doing to set $nav. Then to put it all together, in the final call you would have:

echo ul('navigation', '<!--Before Breakpoint Logo-->'. 
    li('before_breakpoint_logo', '<a href="index.php">
       <img src="logo.png" class="before_breakpoint_logo" /></a>'). $nav);

Upvotes: 1

Related Questions