Reputation: 37934
I am trying to echo some php code in php:
if(isset($_SESSION['id'])){
echo "<li><a href='profile.php'><?php echo $profile ?></a></li>";
}else{
echo "<li><a href='login.php'>login</a></li>";
}
$profile
is in 3 languages thats why it is a variable.
this code isnot working for me. it is echoing nothing. how can i echo php code?
Upvotes: 1
Views: 188
Reputation: 7880
Since you're using double "
s the string will be processed without concatenation.
echo "<li><a href='profile.php'>$profile</a></li>";
You can concatenate if you're dealing with prices in U.S. Currency where you would need the $
character to be literal. But there really is no need outside of code readability.
echo 'Price: $'.$profile;
Code like this affords you the ability to do checks on contents, verifying of information and still keep the dynamic functionality of PHP. Whereas if you mix HTML and PHP you will run into issues where you have to rewrite large sections to make something dynamic in the future.
Ideally you would make everything in PHP:
<?php
$mystring = "<ul>";
if(isset($_SESSION['id'])){
$mystring .= "<li><a href='profile.php'>$profile</a></li>";
}else{
$mystring .= "<li><a href='login.php'>login</a></li>";
}
$mystring .= "</ul>";
$page .= $mystring;
if ($dynamic_site == 1){
print $page;
} else {
//For static caching (much faster)
fwrite ...
}
?>
Upvotes: 2
Reputation: 46
Your mistake here is that you cannot have PHP tags within PHP tags.
If you use double quotes you can simply include the variable directly into the string.
if(isset($_SESSION['id'])){
echo "<li><a href='profile.php'>$profile</a></li>";
}else{
echo "<li><a href='login.php'>login</a></li>";
}
Problem solved.
Upvotes: 1
Reputation: 47127
One more answer is needed:
<?php
echo isset($_SESSION['id']) ? '<li><a href="profile.php">' . $profile . '</a></li>' : '<li><a href="login.php">login</a></li>';
?>
Upvotes: 2
Reputation: 1163
if(isset($_SESSION['id'])){
echo '<li><a href="profile.php">' . $profile . '</a></li>';
}else{
echo '<li><a href="login.php">login</a></li>';
}
Upvotes: 1
Reputation: 840
You have a choice. You can either use double quotes ("
) or single quotes ('
). The difference is that double quotes don't require concatenation:
Double quotes:
echo "<li><a href='profile.php'>$profile</a></li>";
Single quotes:
echo '<li><a href="profile.php">' . $profile . '</a></li>';
Upvotes: 3
Reputation: 18843
This is a redundant answer because everyone else already said it, but you really should avoid printing html in php. Instead, break out of php to write html:
<?php if(isset($_SESSION['id'])): ?>
<li><a href='profile.php'><?php echo $profile ?></a></li>
<?php else: ?>
<li><a href='login.php'>login</a></li>
<?php endif; ?>
Not that it doesn't work the other way, it's just cleaner this way... according to general opinion I suppose.
Upvotes: 2
Reputation: 59709
Just use the concatenation operator .
, like this:
echo "<li><a href='profile.php'>" . $profile . "</a></li>";
Upvotes: 4
Reputation: 1523
if(isset($_SESSION['id'])){
echo "<li><a href='profile.php'>$profile</a></li>";
}else{
echo "<li><a href='login.php'>login</a></li>";
}
Upvotes: 3
Reputation: 50808
Escape the interpreter.
echo "<li><a href='profile.php'>", $profile ,"</a></li>";
that's all.
Upvotes: 2