有条件地在HTML代码块之间嵌入HTML? [英] Conditional embed HTML between PHP code blocks?

查看:129
本文介绍了有条件地在HTML代码块之间嵌入HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP相当陌生。我从3周前开始学习它。我无法在StackOverflow,Google或Youtube上找到此问题的答案。这个 PHP文档让我感到困惑。为了解决这个问题,PHP代码如何与HTML混合工作?

I'm fairly new to PHP. I started learning it like 3 weeks ago. I cannot find the answer to this question on StackOverflow, Google or Youtube. The PHP documentation to this just confuses me. To get on with the question, how does PHP code mixed in with HTML work?

<?php if (something) { ?>
    <p>Hello</p>
<?php } ?>

p元素只会在真值为真时显示,这是怎么回事?... I确信PHP引擎忽略了代码块外部发生了什么(例如<?php?>),并且只解析了内部发​​生了什么。

The p element will only display if something has a truthy value, how is this?... I thought for sure that the PHP engine ignored what was going on around the outside of the codeblocks (e.g. <?php ?>) and only parsed what happens on the inside.

下面的代码被PHP引擎正常解析并发送到浏览器,而不会影响任何HTML元素(即使它明显在2个代码块之间)。

The code below gets parsed by the PHP engine normally and sent to the browser without affecting any HTML elements (even though its clearly between 2 code blocks).

<?php echo $something; ?>
<p>Hello</p>
<?php echo $something; ?>

我希望我不会因为很多人似乎对这个问题感到愤怒了解它如何在十分之一秒内工作。

I hope I'm not going to get flamed for asking this question since a lot of people seem to understand how it works in like a tenth of second.

PS我很早就提出了这个问题,并认为我正确地理解了这个问题,但是当我执行它时,我的头脑仍然像这样,它是如何工作的?这只是对我的一种破解。

P.S. I asked this question in chat early and thought I understood it correctly but when I went to implement it my mind was still like, how does this work exactly? It just seems like some kind of hack to me.

推荐答案

现在很简单。绝对需要一个PHP教程,以便您开始使用 http://www.tizag.com/phpT/

Easy now. Definitely need a php tutorial for you to start on http://www.tizag.com/phpT/

以下是您的所作所为:

Here is what your doing:

<?php 
//Anything inside me php processes
if($something)
{
    echo "<p>something</p>";
}
//About to stop processing in php
?>
<p>Anything outside of the php statement above will just be printed to the dom</p>

快速提示:将您的PHP与HTML分开是一个很好的习惯

Quick Note: It is good practice to separate your PHP from your HTML

<?php if ($something) { ?>  <-- where is the other {
<p>Hello</p>
<?php } ?> <-- oh I see it.

这篇关于有条件地在HTML代码块之间嵌入HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆