PHP 和 HTML 格式化,超级基本的问题

发布于 2024-11-30 05:11:40 字数 430 浏览 2 评论 0原文

我的代码进入了一个阶段,其中代码过多,并且过多的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

旧竹 2024-12-07 05:11:40

不太确定你在问什么,所以如果我正确理解你的问题,你正在寻找一种用 PHP 打印 HTML 块的方法?

<?php if ($a == $b): ?>
  <div>a == b</div>
  <p>a is equal to b</p>
<?php else: ?>
  <div>a != b</div>
  <p>a is not equal to b</p>
<?php endif; ?>

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?

<?php if ($a == $b): ?>
  <div>a == b</div>
  <p>a is equal to b</p>
<?php else: ?>
  <div>a != b</div>
  <p>a is not equal to b</p>
<?php endif; ?>
宫墨修音 2024-12-07 05:11:40

我通常这样做:

<?
    function print_heading()
    {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><? print_title(); ?></title>
    <link rel="stylesheet" type="text/css" href="..." />
    <script language="javascript" type="text/javascript" src="..."></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="title" content="<? print_title(); ?>" />
</head>
<?
    }
?>

但我认为用 if (condition) 替换 function print_heading() 也可以。

I generally do it like this:

<?
    function print_heading()
    {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><? print_title(); ?></title>
    <link rel="stylesheet" type="text/css" href="..." />
    <script language="javascript" type="text/javascript" src="..."></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="title" content="<? print_title(); ?>" />
</head>
<?
    }
?>

But I would think that replacing function print_heading() with if (condition) would work too.

桃酥萝莉 2024-12-07 05:11:40

还有一种替代语法:

<?php if($x): ?>
do_a_lot_of_html_stuff;
<?php endif; ?>

There's an alternative syntax:

<?php if($x): ?>
do_a_lot_of_html_stuff;
<?php endif; ?>
℉絮湮 2024-12-07 05:11:40

您可以在 PHP 条件之间添加 HTML,如下所示:

<?php 
$a=1;
if($a==1){ ?>
<div>All the HTML Stuffs</div>
<?php } ?>

You can add HTML between PHP conditions as follows:

<?php 
$a=1;
if($a==1){ ?>
<div>All the HTML Stuffs</div>
<?php } ?>
静谧幽蓝 2024-12-07 05:11:40

你可以这样做:

<?php if( cond ) { ?>
    SECRET!
<?php } ?>

You can do something like:

<?php if( cond ) { ?>
    SECRET!
<?php } ?>
萌化 2024-12-07 05:11:40

我建议您查看模板引擎,许多 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.

狂之美人 2024-12-07 05:11:40

我猜你的代码现在应该使用面向对象编程。
过程式编码很好,但您最终会达到页面中代码过多的地步。

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.

满天都是小星星 2024-12-07 05:11:40

好吧,你可以使用类似 ac 的语法,并且仍然可以做很多类似的事情。

这是使用类似 C 语法的 if 的典型 php 方法。

<?php
    if(1==1):
        echo 'i can do lots of stuff here';
        $variable = 'i hold some value';
        $array = array('1','two','three');
    endif;
?>

另一种可以实现的方法是使用括号。例如。

<?php
if(condition) {
   //do some stuff here
} else if(cond) {
   //do another stuff here based on some conditions 
} else if(cond) {
    //you can extend the nested elseif as many times as you like
} else {
    //else execute this.
}
?>

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.

<?php
    if(1==1):
        echo 'i can do lots of stuff here';
        $variable = 'i hold some value';
        $array = array('1','two','three');
    endif;
?>

another way you could implement is by using brackets. for example.

<?php
if(condition) {
   //do some stuff here
} else if(cond) {
   //do another stuff here based on some conditions 
} else if(cond) {
    //you can extend the nested elseif as many times as you like
} else {
    //else execute this.
}
?>
单挑你×的.吻 2024-12-07 05:11:40

猜猜你是在问是否有适用于 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文