Can You Put PHP Inside PHP with Echo

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

Can I put ?php inside p tag?

Yes, you can do that. No issues that I see. PHP code would work similarly.

Example :-

<p><?php echo $myName; ?></p>

and

<?php echo $myName; ?>
<p>Something else here</p>

both will work correctly.

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.

PHP Variable inside PHP - Echo - Javascript

You'll want to use string concatenation (using the . character) to insert a variable into your string. Like this:

echo '
[...]
<script type="text/javascript">
var dID = ' . $dID . ';
function wait(ms){
[...]
';

A . will concatenate two strings together. For example:

echo 'hello ' . ' world'

You can also insert a variable directly into a string, if you use double quotes. Single quotes do not allow you to do this:

$text = "world";
echo "hello $text";

In general, you should wrap your variables in curly brackets ({ and })

$text = "world";
echo "hello {$text}";

Is it possible to run PHP include() inside an echo?

No. echo implicity calls __toString() on whatever is contained within the statement to be echo'd.

What you should do is include the file and have the HTML in the included file. No need to echo.

Nesting PHP inside PHP

Just concat as below

 echo "<a class='btn btn-primary' href='single.php?news=".$row['codn']."'>Read more...</a>";


Related Topics



Leave a reply



Submit