简体   繁体   中英

PHP: Execute php code inside “php echo”

I've got a php code where I created an echo:

<?php 
  my code...
  $line = "Hello world";
  echo "<li>php if (!empty($line)) { echo $line; }</li>";
?>

Unfortunately the <li> does not show Hello World. As you can see here: http://codepad.org/QfcsINDy

Your syntax is wrong. Check this

<?php 
 my code...
 ?>
<li><?php echo 'Hello World'; ?>

<?php
?>

Or just

<?php 
 my code...
 echo "<li>Hello World</li>";
?>

you can simply write below code

<?php
 echo "<li>Hello World</li>";
?>

or

<li><?php echo "Hello World";?></li>
<li><?php echo 'Hello World'; ?></li>

only use php where necessary. i am assuming you need to replace this 'Hello World' with a variable eventually thats why I used php, otherwise it would be like this.

<li>Hello World</li>

Try this..

<?php 
 my code...

echo '<ul>';
echo '<li> Your Static Text Here </li>';
echo '</ul>';

?>

echo cannot be part of a php expression neither can it be used on a php expression. Just simply do

echo "<li>Hello World!</li>";

Or

print "<li>Hello World!</li>;

If what you are trying to achieve is have an expression between html tags please do

<li><?php echo "hello world" ?></li>

Or

<li><?= "hello world" ?></i>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM