如何创建一个php将解析的变量?

Is there any way in php to define a variable like...

$content = if ($user['adminLevel'] > 0) echo '<p>YAY IT WORKS</p>';

And when you echo the variable it will perform the if statement?

How about just writing a function? Then instead of "echoing the variable", just call the function.

function content() {
    global $user;
    if ($user['adminLevel'] > 0) {
        echo '<p>YAY IT WORKS</p>';
    }
}


// and then later on...
content();

I don't think that is possible, but you could create a function.

function adminLevel($user) {
    if($user['adminLevel'] > 0) {
        return '<p>YAY IT WORKS!</p>';
    }
    else {
        return '';
    }
}

then you just call it in your php.

$content = adminLevel($user);

You can use a ternary operator

$content = $user['adminLevel'] > 0 ? '<p>YAY IT WORKS</p>' : null;
echo $content;

This works like the following

$var = condition ? 'true value' : 'false value';

http://tuxradar.com/practicalphp/3/12/4