Well, for starters, you might not wanna overuse echo, because (as is the problem in your case) you can very easily make mistakes on quotation marks.
This would fix your problem:
echo "<a href=\"http://www.whatever.com/$param\">Click Here</a>";
but you should really do this
<?php
$param = "test";
?>
<a href="http://www.whatever.com/<?php echo $param; ?>">Click Here</a>
Answer from nicolaas on Stack OverflowUsing PHP variables inside HTML tags? - Stack Overflow
How to Include PHP code in between HTML tags? - Stack Overflow
How to write html code inside <?php ?> block? [closed]
How Do You Use PHP To Modify HTML Tags?
Videos
Well, for starters, you might not wanna overuse echo, because (as is the problem in your case) you can very easily make mistakes on quotation marks.
This would fix your problem:
echo "<a href=\"http://www.whatever.com/$param\">Click Here</a>";
but you should really do this
<?php
$param = "test";
?>
<a href="http://www.whatever.com/<?php echo $param; ?>">Click Here</a>
There's a shorthand-type way to do this that I have been using recently. This might need to be configured, but it should work in most mainline PHP installations. If you're storing the link in a PHP variable, you can do it in the following manner based off the OP:
<html>
<body>
<?php
$link = "http://www.google.com";
?>
<a href="<?= $link ?>">Click here to go to Google.</a>
</body>
</html>
This will evaluate the variable as a string, in essence shorthand for echo $link;
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
}
?>
You can do like
HTML in PHP :
<?php
echo "<table>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>".$name."</td>";
echo "</tr>";
echo "</table>";
?>
Or You can write like.
PHP in HTML :
<?php /*Do some PHP calculation or something*/ ?>
<table>
<tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr>
</table>
<?php /*Do some PHP calculation or something*/ ?>
Means:
You can open a PHP tag with <?php, now add your PHP code, then close the tag with ?> and then write your html code. When needed to add more PHP, just open another PHP tag with <?php.
You can drop in and out of the PHP context using the <?php and ?> tags. For example...
<?php
$array = array(1, 2, 3, 4);
?>
<table>
<thead><tr><th>Number</th></tr></thead>
<tbody>
<?php foreach ($array as $num) : ?>
<tr><td><?= htmlspecialchars($num) ?></td></tr>
<?php endforeach ?>
</tbody>
</table>
Also see Alternative syntax for control structures
Yes I know you can use PHP to create your own HTML tags and content.
But what I want to know is this: How do you insert code inside an existing HTML tag to change the inner code of it?
Here's an example. Lets say you have a div tag with an inline style attribute. You want to use PHP code to change the color of the content inside the div. So in your block of PHP code you will generate a string like ''blue" and return that string. Then you will insert that string into the inline style attribute, and the color of the div will change.
How do you do that?
Whenever I try it, the string just gets outputted as text on the page itself.....it doesn't actually change the color of the div.
What am I doing wrong?
So far this is what I've tried. My php function returns just the color as a string. So this should insert that color into the inline style right? I've made sure to add double quotes as part of the string too so that it fits the conventions of inline styles.
<div style=<?php echo '"color: '.generateColor().'"'; ?>>
That's a HEREDOC syntax , The closing HTML; should be in margin else it wont work.
Valid Syntax
<?php
$somevar="Hello World";
echo <<<HTML
<b> Hey this is some text and I am adding this variable $somevar</b>
HTML;
Invalid Syntax
<?php
$somevar="Hello World";
echo <<<HTML
<b> Hey this is some text and I am adding this variable $somevar</b>
HTML; //<--- As you can see there is a leading space
You can always use <script type="text/php">...</script>
You can do like
HTML in PHP :
<?php
echo "<table>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>".$name."</td>";
echo "</tr>";
echo "</table>";
?>
Or You can write like.
PHP in HTML :
<?php /*Do some PHP calculation or something*/ ?>
<table>
<tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr>
</table>
<?php /*Do some PHP calculation or something*/ ?>
Means:
You can open a PHP tag with <?php, now add your PHP code, then close the tag with ?> and then write your html code. When needed to add more PHP, just open another PHP tag with <?php.
You can drop in and out of the PHP context using the <?php and ?> tags. For example...
<?php
$array = array(1, 2, 3, 4);
?>
<table>
<thead><tr><th>Number</th></tr></thead>
<tbody>
<?php foreach ($array as $num) : ?>
<tr><td><?= htmlspecialchars($num) ?></td></tr>
<?php endforeach ?>
</tbody>
</table>
Also see Alternative syntax for control structures