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

I'm trying to if else condition inside 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 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 }?>

but, following condition is not work properly in above code. What is wrong here?

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

In you code you wrire php code inside php code

<?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 }?>

Try this, you should check the value then print the desired result

<?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 }?>

You can't put if in echo statement but you can put echo in if statement like This:

<?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 }?>

It appears you're trying to echo actual PHP code, rather than the results of that code.

Change the line to <?php if ($direction == 'ltr') { ?><span class='fa fa-caret-right'></span><?php } else { ?><span class='fa fa-caret-left'></span><?php } ?></a>;

Because you are expressing the HTML naturally within the condition, there is no need to echo anything.