PHP 和 HTML 格式化,超级基本的问题
我的代码进入了一个阶段,其中代码过多,并且过多的 HTML 取决于某些服务器条件的后果。
我只是想知道,有什么办法可以绕过:
<?php if (cond) echo '<p class="someclass">some HTML</p>'; ?>
?
我只是希望有类似 C 语言的东西,你可以简单地像:
#ifdef x
do_a_lot_of_html_stuff;
#endif
我所看到的我现在能做的就是像:
<?php if (x) require_once("includes/all_needed_part.php"); ?>
谢谢!
I got to a phase in my code where I have too much code and too much HTML is depended on the consequences of some server conditions.
I simply want to know, is there any way to get around the:
<?php if (cond) echo '<p class="someclass">some HTML</p>'; ?>
?
I just wish there was something like in C where you can simply go like:
#ifdef x
do_a_lot_of_html_stuff;
#endif
All I can see that I can do now is go like:
<?php if (x) require_once("includes/all_needed_part.php"); ?>
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不太确定你在问什么,所以如果我正确理解你的问题,你正在寻找一种用 PHP 打印 HTML 块的方法?
Not exactly sure what you're asking, so if I am understanding your question correctly, you're looking for a way to print off blocks of HTML with PHP?
我通常这样做:
但我认为用
if (condition)
替换function print_heading()
也可以。I generally do it like this:
But I would think that replacing
function print_heading()
withif (condition)
would work too.还有一种替代语法:
There's an alternative syntax:
您可以在 PHP 条件之间添加 HTML,如下所示:
You can add HTML between PHP conditions as follows:
您可以将控制结构的替代语法与heredoc-syntax 。
You may use the alternative syntax for control structures in combination with the heredoc-syntax.
你可以这样做:
You can do something like:
我建议您查看模板引擎,许多 PHP 都有一个模板引擎最成熟的是Smarty。较新(且更清洁)的解决方案之一是 Twig,它由 Symphony 框架。
I would recommend that you check out a template engine, many exist for PHP with one of the most mature being Smarty. One of the newer (and cleaner) solutions is Twig, which is employed by the Symphony framework.
我猜你的代码现在应该使用面向对象编程。
过程式编码很好,但您最终会达到页面中代码过多的地步。
i guess you are at a point in your code where you should use Object Oriented Programming.
procedural coding is good but you'll eventually reach the point where there is just to much code in your page.
好吧,你可以使用类似 ac 的语法,并且仍然可以做很多类似的事情。
这是使用类似 C 语法的 if 的典型 php 方法。
另一种可以实现的方法是使用括号。例如。
well you could use a c like syntax and still can do lots of stuff like.
This is typical php approach of using if using c like syntax.
another way you could implement is by using brackets. for example.
猜猜你是在问是否有适用于 php 的框架?答案是肯定的,这里至少有一个好的答案:http://cakephp.org/
Guessing you are asking if there are frameworks available for php? The answer is yes, and here is at least one good one: http://cakephp.org/