简体   繁体   English

在另一个函数中调用函数

[英]Calling function in another function

I have a function printContent() which prints the arguments and logged() which checks if the user is logged in. 我有一个函数printContent() ,它输出参数,而logged()则检查用户是否已登录。

My point is to do something like this: 我的意思是要做这样的事情:

logged("printContent('TITLE', 'CONTENT', 1)", 1);

It doesn't work. 没用 It should printContent() if user is not logged in, but nothing is happening. 如果用户未登录,但没有任何反应,则应为printContent() If I'll try changing return() into print() it prints the text "printContent(text...)". 如果我尝试将return()更改为print()它将打印文本“ printContent(text ...)”。

Here are these two functions: 这是这两个功能:

function logged($echo = 0, $logout = 0)
{    
        if($_SESSION['user']) 
        {
            if($echo)
            {
                if(!($logout)) return $echo;
                else return false;
            }
            else return true;
        }
        else
        {  
            if($logout == 1) return $echo;
            else return false;
        }
}
function printContent($title, $content, $type = 0){
    if($type == 1){
        echo '<div class="right-box">';
        if($title) echo '<h3>'.$title.'</h3>';
        echo $content.'</div>';            
    }
    else {
        echo '<div class="left-box">';
        if($title) echo '<h2>'.$title.'</h2>';
        echo '<div class="left-box-content">'.$content.'</div></div>';
    }
}

It should printContent() if user is not logged in 如果用户未登录,则应为printContent()

You can try 你可以试试

if(!logged())
{
printContent('TITLE', 'CONTENT', 1);
}

Your logged() function is too complicated; 您的logged()函数太复杂了; the $echo and $logout parameters make the logic extremely hard to follow. $echo$logout参数使逻辑极难遵循。 You should simplify it to just this, doing one thing very well: 您应该将其简化为这样,非常好地做一件事:

function isLoggedIn()
{
    return !empty($_SESSION['user']);
}

Then, the logic becomes quite simple afterwards: 然后,逻辑变得非常简单:

if (!isLoggedIn()) {
    printContent('title', 'content', 1);
}

Play time 上场时间

Being fancy, you could do this since 5.3, though it's a very contrived example of what you could accomplish with anonymous functions: 花哨的是,您可以从5.3开始执行此操作,尽管这是可以使用匿名函数完成的非常人为的示例:

function ifLoggedIn($loggedIn, $loggedOut)
{
    return empty($_SESSION['user']) ? $loggedIn() : $loggedOut();
}

The $loggedIn and $loggedOut parameters are the callback parameters and get executed from inside the function, based on whether the user is logged in or not. $loggedIn$loggedOut参数是回调参数,并根据用户是否登录从函数内部执行。 To use it: 要使用它:

ifLoggedIn(function() {
}, function() {
    printContent('TITLE', 'CONTENT', 1);
});

What you seem to be looking for is a callback. 您似乎正在寻找的是回调。

If I understand you correctly, you wish printContent only to execute, if the user is logged in, correct? 如果我对您的理解正确,那么您希望只执行printContent,如果用户已登录,对吗?

You may want to check this question for further info: How do I implement a callback in PHP? 您可能需要检查此问题以获取更多信息: 如何在PHP中实现回调?

you are trying to execute a plain string. 您正在尝试执行一个纯字符串。 Use 采用

function1( function2() );

that is the same as 那和

$tmp = function2();
function1($tmp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM