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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)