【问题标题】:PHP multiline string with PHPPHP 多行字符串与 PHP
【发布时间】:2012-09-14 03:07:51
【问题描述】:

我需要回显很多 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 }  ?>';
?>

我该怎么做?

【问题讨论】:

  • 你真的是在尝试回显 PHP 代码吗?
  • 除了未转义引号的明显问题之外,您还应该将所有 标记转换为其对应的 html 代码 < >
  • 大多数问这个问题的人都会寻找HEREDOC。你可能想接受这个答案。

标签: php html multiline


【解决方案1】:

您不能在这样的字符串中运行 PHP 代码。它只是行不通。同样,当您“用完” PHP 代码 (?&gt;) 时,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;

【讨论】:

    【解决方案2】:

    你不需要输出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>';
    ?>
    

    【讨论】:

    • 互联网,你每天都给我惊喜 :)*
    【解决方案3】:

    代码中的内部单引号集正在杀死字符串。每当您点击单引号时,它都会结束字符串并继续处理。你会想要这样的东西:

    $thisstring = 'this string is long \' in needs escaped single quotes or nothing will run';
    

    【讨论】:

      【解决方案4】:

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

      $string = <<<HEREDOC
         string stuff here
      HEREDOC;
      

      “HEREDOC”部分就像引号一样,可以是您想要的任何内容。结束标签必须是它所在行的唯一内容,即前后没有空格,并且必须以冒号结尾。欲了解更多信息check out the manual

      【讨论】:

        【解决方案5】:

        为此,您必须删除字符串中的所有 ' 字符或使用转义字符。喜欢:

        <?php
            echo '<?php
                      echo \'hello world\';
                  ?>';
        ?>
        

        【讨论】:

          【解决方案6】:

          使用 PHP 的show_source(); 函数。在 show_source 中查看更多详细信息。我想这是一个更好的方法。

          【讨论】:

            【解决方案7】:

            使用冒号表示法

            另一种选择是使用带有冒号 (:) 的 ifendif,而不是括号:

            <?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>
            

            【讨论】:

              猜你喜欢
              • 2012-04-02
              • 2015-09-18
              • 2018-05-20
              • 1970-01-01
              • 2020-02-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多