douxunnian0423 2013-10-07 02:25
浏览 42
已采纳

使用函数作为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?

  • 写回答

1条回答 默认 最新

  • duanqiao2006 2013-10-07 02:36
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向