在PHP的echo中包含php和html

时间:2013-05-17 15:34:00

标签: php

简单的php真的,因某种原因无法得到它......

<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
    echo 'This is some test content, within which is some more php bits 
    <?php echo 'Test'; ?> and an if statement 
    <?php if ( $anothervalue = "test" ) { echo 'Print this'; } else 
    { echo 'Print that' ; } ?>
    And then some more content
    <br />
    Test
    ?>

} else {
    echo 'The value didnt match';
}
?>

我需要在回声中包含php,我该怎么做?

8 个答案:

答案 0 :(得分:1)

据我所知,你不能。 echo仅用于输出。

改为重构。

<?php
  $myvalue = 'yes'
  if ( $myvalue == 'yes' ) {
?>
This is some test content, within which is some more php bits <?php echo 'Test'; ?> and an if statement 
<?php 
    if ( $anothervalue = "test" ) { 
      echo 'Print this'; 
    } else {
      echo 'Print that' ; 
    } 
?>
And then some more content
<br />
Test
<?php
} else {
    echo 'The value didnt match';
}

答案 1 :(得分:1)

<?php

function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; } 
else { return 'Print that' ; } 
}


$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content


?>

答案 2 :(得分:1)

我建议你先创建一个函数:

<?php

function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; } 
else { return 'Print that' ; } 
}

然后将其包含在您的陈述/文字中;

$myvalue = 'yes'
    if ( $myvalue == 'yes' ) {
    echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
    And then some more content


    ?>

答案 3 :(得分:0)

您必须使用.运算符连接字符串位:

echo 'This is some test content' . ' chained to other stuff' . someFunction() . 'and some other stuff' . $someVariable . ', right?';

答案 4 :(得分:0)

试试这个

<?php
    $myvalue = 'yes';
    if ( $anothervalue == "test" ) { $test = 'Print this'; } else { $test = 'Print that' ; }
    if ( $myvalue == 'yes' ) {
        echo "This is some test content, within which is some more php bits $test
        <br />
        Test";
    }
    else
    {
         echo 'The value didnt match';
    }
?>

答案 5 :(得分:0)

您可以在echo的上下文中表达某些条件文本。您可以将字符串串联(使用逗号或点)与内联条件语法(ternary operators)一起使用,这样就不必从<?php跳入跳出,也不必编写多个回声。 / p>

代码:(Demo

$myvalue = 'yes';
$anothervalue = 'test';
if ($myvalue == 'yes') {
    echo 'This is some test content, within which is some more php bits Test ' , ($anothervalue == 'test' ? 'Print this' : 'Print that') , ' And then some more content<br />Test';
} else {
    echo 'The value didnt match';
}

输出:

This is some test content, within which is some more php bits Test Print this And then some more content<br />Test

答案 6 :(得分:-1)

你可以尝试像

这样的连接
echo "<img src='".$url."' />";

答案 7 :(得分:-2)

试试这个,但我建议你去w3schools并继续学习基础知识。

<?php
  $myvalue = 'yes';
  if ( $myvalue == 'yes' ) {
     echo 'This is some test content, within which is some more php bits Test and an if statement';
  if ( $anothervalue = "test" ) { 
      echo 'Print this'; 
  } 
  else { 
      echo 'Print that'; 
  }
?>
And then some more content
<br />
Test
<?php
   }
   else
   {
      echo 'The value didnt match';
   }
?>