PHP Echo Inside Echo

PHP echo inside echo

That is some of the ugliest code I have ever seen...

<?php 
echo '
<h3>Hello</h3>';

while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>

You could also use the HEREDOC syntax.

How to use echo inside echo

echo '<form method="post" class="product" action="index.php" id="addtocartproduct '. $products->virtuemart_product_id.' ">';

Echo inside echo PHP

You can't have PHP tags between PHP tags. Concat the strings instead:

if (is_single()) { 

echo '<div><p>html</p>' . do_shortcode( '[contact-form-7 id="1" title="contact form"]' ) . '</div>';

} ?>

PHP - echo inside an echo

why did you open a <?php tag again, you are already in echo line?

echo '<td align="left"><a href="'.url('Forum/create_new_post?topic_id='.$post->topic_id.'&forum_id='.$post->forum_id.'').'"><img src="'.SITE_URL.'/lib/skins/flyeuro/images/forums/t_reply.gif"/></a></td>';

and what is SITE_URL? Is that a variable, did you forget to put $?


echo prints out the string that you gave as parameter,

echo "foo";

As @hakre mentioned about it, . is used to concatenate strings.

$var = "foo"."bar";  //foobar

So you can use it in echo line,

$var = "foo"."bar";  //foobar
echo "foo "."bar ".$var // foo bar foobar

And It's not important weather variable defined as a string. It would be a constant variable.

define('SITE_URL', 'localhost:8080/phpvms'); 
echo "my website URL is ".SITE_URL; //my website URL is localhost:8080/phpvms

I'm trying to use 'echo' inside body

you should store value in variable instead of echo when check connection.

$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "hayleyblog";

$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
} else {
$conn = "<p>connected</p>";
}

Then you can echo as below.

<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
</head>
<body>

<?php
echo $conn;
?>
</body>
</html>

Can you put PHP inside PHP with echo?

You cannot have PHP echo more PHP code to be evaluated because PHP interprets your code in a single pass. If you have, say, <?php echo '<?php echo "hello"; ?>'; ?>, You will literally get the text, <?php echo "hello"; ?> as output, and the interpreter will not touch it.

You can, however, jump in and out of PHP at will:

<?php
echo "I am going to be interpreted by PHP.";
?>
I am not interpreted by PHP.
<?php
echo "But I am again.";
?>

If you think that you need to output PHP code that is itself re-evaluated, there is always a better way to accomplish this. If you give a specific example of what you are trying to accomplish (real-world case), then the folks here on SO would be happy to help.

Running PHP inside of PHP echo?

Okay, there are few mistakes in this script.

First of all, you are using php tags inside php tags, it doesn't make sense, you are already using php so you don't need those php tags.

But even if you remove the php tags it won't work because you are inside a string, so you are asking php to write litteraly get_site_url() (you are not calling get_site_url(), you are litteraly writing get_site_url())

What should you do then ?

Lets see how concatenation work first. The concatenation operator is ".". It allows to concatenate two string.

Example :

 $sentence = "Hello" . " " . "Thierry"; // means $sentence = "Hello Thierry".

Okay now lets do the same with variable.

$name = "Thierry";
$sentence = "Hello" . " " . $name; // means $sentence = "Hello Thierry";

This is everything you need here.

Lets see how we can solve your problem then,

what you want as a result is :

echo '<a href="yourSiteUrl/login" class="typcn typcn-key-outline">Log in</a>';

Now we replace yourSiteUrl with the concatenation operator and the php function. And you have :

echo '<a href="'.get_site_url().'/login" class="typcn typcn-key-outline">Log in</a>';

Repeat the process and you'll end up with this :

<?php if ( !is_user_logged_in() ) { 
echo '<a href="'.get_site_url().'/login" class="typcn typcn-key-outline">Log in</a>';
}
else {
echo '<a href="'.get_site_url().'/wp-login.php?action=logout" class="typcn typcn-key-outline">Log out</a>';
} ?>

Hope the english isn't too bad



Related Topics



Leave a reply



Submit