How to Include PHP code in between HTML tags?

You can come in and out of PHP at will:

<?php if(isset($_SESSION['email'])) { ?>
  <a href="http://localhost/ci/myads_view">MY ADS</a>
<?php } ?>

In the above, if the PHP if statement evaluates to true, the HTML within the conditional will execute, otherwise it will not.

In your particular case, I can only assume this is what you're trying to do:

<nav class="main-navigation dd-menu toggle-menu" role="navigation">
  <ul class="sf-menu">
    <li>
      <?php if(isset($_SESSION['email'])) { ?>
        <a href="http://localhost/ci/myads_view">MY ADS</a>
      <?php } else { ?>
        <a href="http://localhost/ci/other-link">OTHER LINK</a>
      <?php } ?>
    </li>
  </ul>
</nav>

Or something along the lines of the above?


PHP is pretty cool, you can stop/resume it mid-document. For example like this, I hope this is the solution to what you were trying to do:

<?php
  if(isset($_SESSION['email']))
  { /* this will temporarily "stop" php --> */ ?>
      <a href="http://localhost/ci/myads_view">
          <nav class="main-navigation dd-menu toggle-menu" role="navigation">
          <ul class="sf-menu">
      </a>
    <?php /* <-- php resumes now */
  }
?>

So what's going on?

First, you write your IF statement and the curly brackets, the code inside of which would usually be executed if the IF statement evaluates to TRUE.

<?php
  if(something==true)
  {
    echo "write something to the page";
  }
?>

But instead of using PHP commands, you quickly jump out of PHP by closing the PHP tag ?> as you would do at the end of your PHP code anyway.

<?php
  if(something==true)
  {
    ?>

Now you can continue in plain HTML without all the usual related issues of PHP commands as escaping characters like ' and " and \ watching your "qoute level" and so on. Instead, you write bog standard HTML.

When you're done entering the HTML that should be output in case the IF statement is true, just open another PHP tag and close the IF statement's curly brackets and, if that's the end of your PHP, close the PHP tag as well.

    <?php
  }
?>