How to place html inside php code

I need to put the PHP code below between the HTML tags. I am confused about how to do this. Anyone have a solution?

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"> 

For this I use frameworkignign framework.

+5
source share
5 answers

You can enter and exit PHP at your discretion:

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

In the above example, if the PHP if statement is true, the HTML in the conditional expression will execute, otherwise it will not.

In your specific case, I can only assume that this is what you are 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 in the lines above?

+2
source

PHP is pretty cool, you can stop / resume it in the middle of the document. For example, for example, I hope this is a 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 is going on?

First, you write your IF statement and curly braces, the code inside which is usually executed if the IF statement evaluates to TRUE.

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

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

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

Now you can continue in plain HTML without all the usual problematic PHP commands, such as escaping characters like "and" and "viewing" qoute level, etc. Instead, you write standard HTML code.

When you finish entering the HTML code that should be output if the IF statement is true, just open another PHP tag and close the curly braces of the IF statement, and if this is the end of your PHP, close the PHP tag as well.

  <?php } ?> 
+1
source

What you did right, you can additionally write as follows:

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

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

0
source

To put a link inside the navigation, you can do it like this, everything inside the same PHP file.

 <?php if(isset($_SESSION['email'])) { $navLink= '<a href="http://localhost/ci/myads_view">MY ADS</a>'; } ?> <nav class="main-navigation dd-menu toggle-menu" role="navigation"> <ul class="sf-menu"><?php echo($navLink); ?> 
0
source

This should work:

 <?php if(isset($_SESSION['email'])): ?> <nav class="main-navigation dd-menu toggle-menu" role="navigation"> <ul class="sf-menu"> <?php endif; ?> 
0
source

Source: https://habr.com/ru/post/1243869/


All Articles