如何在echo内打印PHP行

时间:2014-01-06 12:28:05

标签: php html mysql css navigation

我正在使用PHP创建动态导航,我想在php代码中插入一个php代码 在<li>上,我想在列表项

中插入这行代码
<?php printf('<li' if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';>); printf('%s %s </a></li> ', $row['name'],$row['DESCRIPTION']); } ?>

6 个答案:

答案 0 :(得分:0)

这种方式不会更好(?):

$content = "<li ";
if (strpos($_SERVER['PHP_SELF'], 'index.php')){
    $content .= "class='current'";
}
$content .= "> </a></li>" . $row['name'] . " " . $row['description'];

答案 1 :(得分:0)

尝试这种方式:

<?php 
$index = ((strpos($_SERVER['PHP_SELF'], 'index.php')) ? true : false);
printf('<li' . (($index) ? 'class="current"' : '' ) . '%s %s </a></li> ', $row['name'],$row['DESCRIPTION']);  
?>

答案 2 :(得分:0)

<?php
printf('<li' . (strpos($_SERVER['PHP_SELF'], 'index.php')) ? 'class="current"' : '' . '>');
printf('%s %s </a></li> ', $row['name'], $row['DESCRIPTION']);
?>

答案 3 :(得分:0)

$is_current = '';
if(strpos($_SERVER['PHP_SELF'], 'index.php')){ $is_current = 'class="current"'; }

printf('<li'.$is_current.'>%s %s </a></li>', $row['name'],$row['DESCRIPTION']);

答案 4 :(得分:0)

不完全清楚你在问什么,但我想你可以得到你想要的东西,例如,

<?php 
   echo "<li ";
   if (strpos($_SERVER['PHP_SELF'], 'index.php')) {
       echo ' class="current"';
   }
   // missing: '><a ...>'
   printf(' %s %s </a></li> ', $row['name'], $row['DESCRIPTION']); 
?>

(最后}没有开场{,我刚刚剥离它,却没有问自己是否必须是缺失开场的} { if)。

或者您可以使用?:运算符。

答案 5 :(得分:0)

有关代码中的良好做法和最佳识别,请尝试以下操作:

if (strpos($_SERVER['PHP_SELF'], 'index.php')){ 
    $class = "";
}else{
    $class = "current";
}
printf('<li class="current"> %s %s </a></li>', $row['name'], $row['DESCRIPTION']);