Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include PHP code in between HTML tags? [closed]

currently I'm developing a application using Codeigniter framework and PHP. In there I need to include below mentioned PHP code in between HTML tags. Can anyone provide a solution or an guidance for this?

PHP code :
             if(isset($_SESSION['email'])) 
                {  
                    echo '<a href="http://localhost/ci/myads_view">';
                    echo 'MY ADS' ;
                    echo '</a>';
                }
HTML code:
         <nav class="main-navigation dd-menu toggle-menu" role="navigation">
             <ul class="sf-menu"/>
            </nav>
like image 903
siyumi_amarasinghe Avatar asked Feb 25 '16 16:02

siyumi_amarasinghe


People also ask

How do you add HTML code between PHP codes?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.

How do you close a PHP tag in HTML?

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

Do I need to close <? PHP?

It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response.

Between which tags is PHP code written?

php and ?>. These are called PHP's opening and closing tags. Statements witihn these two are interpreted by the parser. PHP script within these tags can be embedded in HTML document, so that embedded code is executed on server, leaving rest of the document to be processed by client browser's HTML parser.


3 Answers

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?

like image 104
David Wilkinson Avatar answered Sep 28 '22 19:09

David Wilkinson


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
  }
?>
like image 28
Rob Avatar answered Sep 28 '22 19:09

Rob


What you have done is correct, additionally you can write as follows:

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo $str;

Reference - http://php.net/manual/en/language.types.string.php

like image 33
Peeje Avatar answered Sep 28 '22 17:09

Peeje