开发者

How can I make a variable that php will parse?

开发者 https://www.devze.com 2023-04-10 13:31 出处:网络
Is there any way in php to define a variable like... $content = if ($user[\'adminLevel\'] > 0) echo \'<p>YAY IT WORKS</p>\';

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 statemen开发者_Python百科t?


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消