本文介绍了这是将HTML放入PHP的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,在大多数情况下,我都是在回显HTML。今天,我发现这样做的方法使事情变得简单了1000倍:

I'm new to PHP, and most of the time I have been 'echo'ing my HTML. Today, I found this way of doing it, which makes things 1000 times easier:

<?php if(get_field('field_name')){ ?>

   <p>HTML can go here without echo'ing it</p>

<?php } ?>

但这是一种可以接受的方式吗?它似乎每次都起作用。谢谢。

But is it an accepted way? It seems to work every time. Thanks.

推荐答案

主要与您喜欢如何阅读代码有关。



对于具有自动完成/代码建议的IDE,最好使用
<?php if(...){?> ,以便自动完成/ suggestion功能知道何时使用html以及何时为您的代码提供php建议。当然,最好选择易于阅读的编码风格。

据我所知,在

It's mostly about how you like to read your code.

With IDE that has autocomplete/code suggestions it is best to use<?php if (...) { ?> so that autocompletion/suggestion features know when to use html and when php suggestions for your code. And of course it's nice to select coding style that is easy to read later.
As far as I know code highlight wont work, and should not work, inside

echo '<div class="c"><h2>...';



不同的编码样式:



除已经提到的内容没有太大差异。例如,使用

Different coding styles:

Other than what already mentioned there is no big differences. For example using

echo "<div>$var1<h1>$var2</h1>...";

插入变量更容易(更少编写),代码更小足迹,而且比这更容易阅读:

for inserting variables it is easier (less writing), code has smaller footprint and easier to read than this:

<?php
// <= Some code...
?><div><?php echo $var1; ?><h1><?php echo $var2; ?></h1>...



短标记/为什么不使用:

还有<?=?> 快捷方式可用,但由于它需要PHP配置,所以我没有使用它(并非总是可用/可更改/可写)值 short_open_tag 进行设置。但是PHP手册说 自PHP 5.4.0起,<?=始终可用。

请参见

Short open tags / why not use:

There is also <?= ?> shortcut available but I have not used it since it requires PHP configuration (which is not always available/changeable/writable) value short_open_tag to be set. However PHP manual says Since PHP 5.4.0, <?= is always available.
See Are PHP short tags acceptable to use?
and PHP echo vs PHP short tags

<?php?> 中的所有内容均由PHP解析:超文本预处理器,因此有人可能会说使用需要更多的资源。 echo'< html>< here>'; ,但是如果您没有超过100000 echo ,这也是进行优化的错误位置或 print 的+开头<?php 并结束?> echo 一样,也很少使用指令,因此在计算cpu指令时,它们几乎相同,至少在我的安装中。

请参见



Everything inside <?php ?> is parsed by PHP: Hypertext Preprocessor so someone may say that it takes much more resources to use echo '<html><here>'; but this is again wrong place to do optimization if you don't have over 100000 echo's or print's + starting <?php and ending ?> takes few instructions too as does echo so they are almost same when counting cpu instructions, at least with my installation.
See How exactly is a PHP script executed?
and php.net/manual/en/tokens.php
and PHP Compiler Internals slides by Sebastian Bergmann

1.77s .: 1000000次 $ var = rand(0 ,1); echo‘Foo Bar’。$ var;

1.61s .: 1000000次 $ var = rand(0,1); ?> Foo Bar<?php echo $ var; ?><?php

1.83s .: 1000000次 $ var = rand(0,1); echo Foo Bar $ var;

1.77s.: 1 000 000 times $var = rand(0, 1); echo 'Foo Bar '.$var;
1.61s.: 1 000 000 times $var = rand(0, 1); ?>Foo Bar <?php echo $var; ?><?php
1.83s.: 1 000 000 times $var = rand(0, 1); echo "Foo Bar $var";

我真的认为这与您喜欢如何编写/阅读您的作品有关码。有时最好写一些,以后再阅读
if(...){echo something ;; }
有时是
<?php if(...){?> something<?php}?>

I really think that it is most about how you like to write/read your code. Sometimes it's better to write and later read
if (...) { echo "something"; }
and sometimes
<?php if (...) { ?>something<?php } ?>

我个人更喜欢使用 echo ...; ,当有很多变量或输出大块的html或仅包含少量变量的长文本时,仅打印出一小段文本和?> ...<?php

Personally I prefer using echo "..."; when there is a lot of variables or only small piece of text printed out and ?>...<?php when outputting large chuncks of html or long text with only few variables. This makes code easier to read.

如果我做对了,php从这个角度来看,首先进行词法分析并将结果传递给加速器,?> text<?php echo text; 有很大的不同,但仍然不会改变普通网站项目中的性能。
更清楚:加速器本身确实可以提高性能,但是要使用?> text<?php echo text; 没什么大不同。

If I got it right, php does lexical analysis first and passes results to accelerator, from this point of view ?>text<?php and echo "text"; has big difference but it still does not change performance within normal website projects.
More clear: Accelerator itself does improve performance but about using ?>text<?php or echo "text"; does not make big difference.

是的,它已经被广泛接受和使用,但是我认为(至少从PHP文档中没有发现任何明确的内容),它仍然是未记载的方式,但是,如您所知,这不仅是方法。基本上,它的工作和使用echo一样:

Yes, it is widely accepted and used but I think (at least did not find anything clear from PHP docs) it is still undocumented way, However, as you already know it is not only way. Basically it does just same job as if using echo:

if ( get_field('field_name') ) {
   echo "<p>HTML can go here by echo'ing it</p>";
}

在尝试查找正式文档时,我已阅读以下页面:







使用了google,并搜索了一些论坛。

While trying to find official docs I have read these pages:
php.net/manual/en/control-structures.alternative-syntax.php
php.net/manual/en/language.basic-syntax.phpmode.php
php.net/manual/en/language.types.string.parsing.complex
used google, and searched some forums.

似乎不太经常使用记录清晰的备用方法:

It seems that clearly documented alternate way is not used so often:

<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>



更新:



有关分离的官方文档 {} <?php 和<$条件语句中的c $ c>?> 在这里:和一起对其进行了文档记录,但是仅针对替代方式进行了演示(见上文)。

UPDATE:

Official documentation about separating { and } by <?php and ?> within conditional statements is here: control-structures.intro and language.basic-syntax.phpmode.php together documented it, however there is demonstration only for alternate way to do it (see above).

这篇关于这是将HTML放入PHP的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 09:06