0

I have a function printContent() which prints the arguments and logged() which checks if the user is logged in.

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. If I'll try changing return() into print() it prints the text "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>';
    }
}
George Cummins
  • 28,485
  • 8
  • 71
  • 90
MuchaZ
  • 391
  • 4
  • 18
  • 1
    `"printContent('TITLE', 'CONTENT', 1)"` is a string which you're passing into $echo. Then you return $echo, which is just a string. Your `if($echo)` check isn't doing what you want because $echo exists, but as a string. – nwalke May 28 '13 at 16:57
  • Why on Earth should *strings* be evaluated as PHP code? – Álvaro González May 28 '13 at 16:58
  • 1
    Note that `printContent()` isn't returning data: it is only echoing it. If you want to use the output from that function as input to another, you will need to `return` the data. – George Cummins May 28 '13 at 16:59
  • 2
    So your `logged()` function may return a ___string___ containing "printContent('TITLE', 'CONTENT', 1)"... why do you think a string should return the result of another function call just because it contains the name of another function? – Mark Baker May 28 '13 at 16:59
  • 3
    Now just waiting to downvote the first answer that suggests using `eval()` – Mark Baker May 28 '13 at 17:00
  • @MarkBaker: I'm happy to see that no one has done so yet! :-D – gen_Eric May 28 '13 at 17:02
  • Thought about it just to see how many downvotes one can acquire in a short period of time – nwalke May 28 '13 at 17:03

4 Answers4

4

It should printContent() if user is not logged in

You can try

if(!logged())
{
printContent('TITLE', 'CONTENT', 1);
}
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

Your logged() function is too complicated; the $echo and $logout parameters make the logic extremely hard to follow. 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:

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. To use it:

ifLoggedIn(function() {
}, function() {
    printContent('TITLE', 'CONTENT', 1);
});
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

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?

You may want to check this question for further info: How do I implement a callback in PHP?

Community
  • 1
  • 1
Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
0

you are trying to execute a plain string. Use

function1( function2() );

that is the same as

$tmp = function2();
function1($tmp);
Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44
  • 1
    Problem is, this will run `function2` *before* `function1` is ran, and that may not be what the OP wants. – gen_Eric May 28 '13 at 17:06