0

简单的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';
}
?>

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

4

8 回答 8

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


    ?>
于 2013-05-17T15:42:13.817 回答
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';
}
于 2013-05-17T15:37:58.127 回答
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


?>
于 2013-05-17T15:38:19.067 回答
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';
    }
?>
于 2013-05-17T15:42:18.040 回答
0

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

echo 'This is some test content' . ' chained to other stuff' . someFunction() . 'and some other stuff' . $someVariable . ', right?';
于 2013-05-17T15:37:04.790 回答
0

您可以在echo. 您可以将字符串连接(使用逗号或点)与内联条件语法(三元运算符)一起使用,这样您就不必反复进出,<?php也不需要编写多个回显。

代码:(演示

$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
于 2018-11-16T22:09:23.363 回答
-1

你可以尝试像这样的连接

echo "<img src='".$url."' />";
于 2013-05-17T15:37:46.063 回答
-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';
   }
?>
于 2013-05-17T15:39:53.450 回答