不工作,如果否则条件内回声

时间:2015-09-03 09:58:05

标签: php

我正在尝试如果其他条件内部回声。

<?php if(count($category['children'][$i]['children_level2'])>0){ ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?>
    <?php echo "<?php if ($direction == 'ltr') { ?><span class='fa fa-caret-right'></span><?php } else { ?><span class='fa fa-caret-left'></span><?php } ?></a>";
    } else{ ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a>
    <?php }?>

但是,以下条件在上述代码中无法正常工作。这有什么不对?

<?php if ($direction == 'ltr') { ?><span class='fa fa-caret-right'></span><?php } else { ?><span class='fa fa-caret-left'></span><?php } ?>

4 个答案:

答案 0 :(得分:1)

试试这个,您应该检查值然后打印所需的结果

<?php if(count($category['children'][$i]['children_level2'])>0){ ?>

      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?>
    <?php if ($direction == 'ltr') { print "<span class='fa fa-caret-right'></span>";  } else {  print "<span class='fa fa-caret-left'></span>"; } ?></a>
    <?php
    } else { ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a>
    <?php }?>

答案 1 :(得分:1)

你不能把if放在echo语句中,但你可以在if语句中放入echo:

<?php if(count($category['children'][$i]['children_level2'])>0){ ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>">
    <?php echo $category['children'][$i]['name']; ?>
    <?php if ($direction == 'ltr') { echo"<span class='fa fa-caret-right'></span>"; } else { echo "<span class='fa fa-caret-left'></span> </a>"; }
    } else{ ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a>
    <?php }?>

答案 2 :(得分:1)

在你的代码中,你在PHP代码中破解了PHP代码

<?php if(count($category['children'][$i]['children_level2'])>0){ ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?>
    <?php  if ($direction == 'ltr') { ?><span class='fa fa-caret-right'></span><?php } else { ?><span class='fa fa-caret-left'></span><?php } ?></a>
   <?php } else{ ?>
      <a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a>
    <?php }?>

答案 3 :(得分:0)

您似乎正在尝试回显实际的PHP代码,而不是该代码的结果。

将行更改为 <?php if ($direction == 'ltr') { ?><span class='fa fa-caret-right'></span><?php } else { ?><span class='fa fa-caret-left'></span><?php } ?></a>;

因为您在条件中自然地表达HTML,所以不需要回应任何内容。