条件if在变量内部


Conditional if inside variable

我有nav.php,里面有2个变量。我想通过回显该变量将nav.php包含在index.php中。nav.php代码如下:

<?php 
$TopLeftNav = '<li if ($_SERVER['PHP_SELF'] == "/dashboard.php") echo "class = 'active'" ><a href="dashboard.php">Dashboard</a></li>
<li if ($_SERVER['PHP_SELF'] == "/website.php") echo "class = 'active'" ><a href="website.php">Add Website</a></li>
<li php if ($_SERVER['PHP_SELF'] == "/report.php") echo "class = 'active'" ><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>';
?>

如您所见,变量$TopLeftNav不起作用,因为其中有if条件。该条件用于突出显示当前活动页面。

我知道我可以通过创建单独的文件(例如nav1.phpnav2.php)来使它们工作,并分离导航html,但出于学习目的,我如何解决这个问题?提前谢谢。

您可以这样做:

<?php 
// set the variables we will use to an empty string
$web_class = '';
$dash_class = '';
$report_class = '';
// check the PHP_SELF and set a variable
if ($_SERVER['PHP_SELF'] == "/website.php") {
    $web_class  = 'active';
} elseif ($_SERVER['PHP_SELF'] == "/dashboard.php") {
    $dash_class  = 'active';
} elseif ($_SERVER['PHP_SELF'] == "/report.php") {
    $report_class  = 'active';
}
// generate the output
$TopLeftNav = '<li class="'.$dash_class.'"><a href="dashboard.php">Dashboard</a></li>';
$TopLeftNav .= '<li class="'.$web_class.'"><a href="website.php">Add Website</a></li>';
$TopLeftNav .= '<li class="'.$report_class.'"><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>';
?>

尽管我更倾向于这样做:(切换语句)http://php.net/manual/en/control-structures.switch.php

<?php
// set the variables we will use to an empty string
$web_class = '';
$dash_class = '';
$report_class = '';

switch through the PHP_SELF and set the variables accordingly
switch ($_SERVER['PHP_SELF']){
    case '/dashboard.php':
        $dash_class  = 'active';
        break;
    case '/report.php':
        $report_class  = 'active';
        break;
    case '/website.php':
        $web_class  = 'active';
        break;
}
// generate the output
$TopLeftNav = '<li class="'.$dash_class.'"><a href="dashboard.php">Dashboard</a></li>';
$TopLeftNav .= '<li class="'.$web_class.'"><a href="website.php">Add Website</a></li>';
$TopLeftNav .= '<li class="'.$report_class.'"><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </a>';
?>

switch语句非常适合这类事情,因为当你在几年后回到它时,生成的代码可读性要高得多。

这应该适用于您:

这里我使用一个像这样的临时操作员:

(conditional expression) ? (ouput if true) : (output if false);

<?php 
    $TopLeftNav = '<li  ' . ($_SERVER['PHP_SELF'] == "/dashboard.php" ? ' class = ''active'' ' : '') . ' ><a href="dashboard.php">Dashboard</a></li>
    <li  ' . ($_SERVER['PHP_SELF'] == "/website.php" ? ' class = ''active'' ' : '') . ' " ><a href="website.php">Add Website</a></li>
    <li ' . ($_SERVER['PHP_SELF'] == "/report.php" ? ' class = ''active'' ' : '') . ' " ><a href="report.php">Reports</a></li>';
    $TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </a>';
?>

好的,由于其他答案看起来更像是黑客,而不是有用的东西,请考虑这种方法:

<?php 
// three classes for each for easier adding them to the HTML strings
// each of them will be assigned a value of "class = 'active'" in case of matching urls
// or just an empty string otherwise
$dashboardClass = $_SERVER['PHP_SELF'] == "/dashboard.php" ? "class = 'active'" : "";
$reportClass = $_SERVER['PHP_SELF'] == "/report.php" ? "class = 'active'" : "";
$websiteClass = $_SERVER['PHP_SELF'] == "/website.php" ? "class = 'active'" : "";
$TopLeftNav = "
<li $dashboardClass><a href='dashboard.php'>Dashboard</a></li>
<li $websiteClass><a href='website.php'>Add Website</a></li>
<li $reportClass><a href='report.php'>Reports</a></li>";
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>';
?>

因此,我们的想法是为三个类中的每一个存储要响应的值(因此我们有三个新变量)。我还从单引号切换到了双引号,这样就可以直接插入PHP字符串,而不必连接任何内容,例如,如果PHP_SELF匹配dashboard.PHP,<li $dashboardClass>将变为<li class='active'>,否则仅为<li>

三元运算符:

此:$dashboardClass = $_SERVER['PHP_SELF'] == "/dashboard.php" ? "class = 'active'" : "";与此:相同

if ($_SERVER['PHP_SELF'] == "/dashboard.php") {
    $dashboardClass = "class = 'active'";
}
else {
    $dashboardClass = '';
}