PHP 多行字符串与 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12534836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:44:33  来源:igfitidea点击:

PHP multiline string with PHP

phphtmlmultiline

提问by Matt

I need to echo a lot of PHP and HTML.

我需要回应很多 PHP 和 HTML。

I already tried the obvious, but it's not working:

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

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

How can I do it?

我该怎么做?

回答by Josh

You don't need to output phptags:

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

回答by Marc B

You cannot run PHP code within a string like that. It just doesn't work. As well, when you're "out" of PHP code (?>), any text outside of the PHP blocks is considered output anyway, so there's no need for the echostatement.

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

If you do need to do multiline output from with a chunk of PHP code, consider using a HEREDOC:

如果您确实需要使用一段 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;

回答by noel

Use Heredocs to output muli-line strings containing variables. The syntax is...

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

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

The "HEREDOC" part is like the quotes, and can be anything you want. The end tag must be the only thing on it's line i.e. no whitespace before or after, and must end in a colon. For more info check out the manual.

“HEREDOC”部分就像引号一样,可以是您想要的任何内容。结束标记必须是它所在行上的唯一内容,即前后没有空格,并且必须以冒号结尾。有关更多信息,请查看手册

回答by hitautodestruct

Another option would be to use the ifwith a colon and an endifinstead of the brackets:

另一种选择是使用if带有冒号和 anendif而不是括号的 :

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

回答by Afshin

To do that, you must remove all 'charachters in your string or use an escape character. Like:

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

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

回答by mridul4c

Use the show_source();function of PHP. Check for more details in show_source. This is a better method I guess.

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

回答by usumoio

The internal set of single quotes in your code is killing the string. Whenever you hit a single quote it ends the string and continues processing. You'll want something like:

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

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