PHP的PHP多行字符串

Mat*_*att 36 html php multiline

我需要回应很多PHP和HTML.

我已经尝试了显而易见的,但它不起作用:

<?php echo '
<?php if ( has_post_thumbnail() ) {   ?>
      <div class="gridly-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) ));?></a>
      </div>
      <?php }  ?>

      <div class="date">
      <span class="day">
        <?php the_time('d') ?></span>
      <div class="holder">
        <span class="month">
          <?php the_time('M') ?></span>
        <span class="year">
          <?php the_time('Y') ?></span>
      </div>
    </div>
    <?php }  ?>';
?>
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Jos*_*osh 41

您不需要输出php标签:

<?php 
    if ( has_post_thumbnail() ) 
    {
        echo '<div class="gridly-image"><a href="'. the_permalink() .'">'. the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )) .'</a></div>';
    }

    echo '<div class="date">
              <span class="day">'. the_time('d') .'</span>
              <div class="holder">
                <span class="month">'. the_time('M') .'</span>
                <span class="year">'. the_time('Y') .'</span>
              </div>
          </div>';
?>
Run Code Online (Sandbox Code Playgroud)


Mar*_*c B 39

您不能在类似的字符串中运行PHP代码.它只是不起作用.同样,当你"退出"PHP代码(?>)时,PHP块之外的任何文本都被认为是输出,因此不需要该echo语句.

如果您确实需要使用大量PHP代码进行多行输出,请考虑使用HEREDOC:

<?php

$var = 'Howdy';

echo <<<EOL
This is output
And this is a new line
blah blah blah and this following $var will actually say Howdy as well

and now the output ends
EOL;
Run Code Online (Sandbox Code Playgroud)


noe*_*oel 16

使用Heredocs输出包含变量的多行字符串.语法是......

$string = <<<HEREDOC
   string stuff here
HEREDOC;
Run Code Online (Sandbox Code Playgroud)

"HEREDOC"部分就像引号一样,可以是你想要的任何东西.结束标记必须是它的唯一内容,即之前或之后没有空格,并且必须以冒号结束.有关详细信息,请查看手册.


hit*_*uct 5

使用冒号表示法

另一种选择是使用if带有冒号 ( :) 的 和endif代替方括号:

<?php if ( has_post_thumbnail() ): ?>
    <div class="gridly-image">
        <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )); ?>
        </a>
    </div>
<?php endif; ?>

<div class="date">
    <span class="day"><?php the_time('d'); ?></span>
    <div class="holder">
        <span class="month"><?php the_time('M'); ?></span>
        <span class="year"><?php the_time('Y'); ?></span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)