惯用的 PHP 网页创建

发布于 2024-08-29 12:45:39 字数 754 浏览 3 评论 0原文

我的 PHP 经验相当有限。我刚刚继承了一些对我来说看起来很奇怪的东西,我想知道这是否是一种标准的做事方式。在浏览器位置(例如 www.example.com/example_page)中显示的页面具有类似以下内容:

<?
$title = "Page Title";
$meta = "Some metadata";
require("pageheader.inc");
?>
<!-- Content -->

然后 pageheader.inc 具有类似以下内容:

<?
@$title = ($title) ? $title : "";
@$meta = ($meta) ? $meta : "";
?>
<html>
<head>
<title><?=$title?></title
</head>
<!-- and so forth -->

也许其他人发现这种样式有用,但它让我感到困惑。我想这可能是迈向基本内容管理系统的一步,但它在这里的工作方式我认为它增加了服务器必须做的处理,而不会减少网络开发人员的负担,足以使其值得付出努力。

那么,这是使用 PHP 创建页面的正常方法吗?或者我应该把所有这些都转向更好的方法?

另外,我知道“”(相对于“”)是不可取的;我只是重现代码中的内容。

My PHP experience is rather limited. I've just inherited some stuff that looks odd to me, and I'd like to know if this is a standard way to do things. The page which shows up in the browser location (e.g. www.example.com/example_page) has something like:

<?
$title = "Page Title";
$meta = "Some metadata";
require("pageheader.inc");
?>
<!-- Content -->

Then pageheader.inc has stuff like:

<?
@$title = ($title) ? $title : "";
@$meta = ($meta) ? $meta : "";
?>
<html>
<head>
<title><?=$title?></title
</head>
<!-- and so forth -->

Maybe others find this style useful, but it confuses me. I suppose this could be a step toward a rudimentary content management system, but the way it works here I'd think it adds to the processing the server has to do without reducing the load on the web developer enough to make it worth the effort.

So, is this a normal way to create pages with PHP? Or should I pull all this in favor of a better approach?

Also, I know that "<?" (vs. "<?php" ) is undesirable; I'm just reproducing what is in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

情深缘浅 2024-09-05 12:45:39

嗯,这当然不是一种无效的方法 - 但阅读和维护起来可能有点麻烦。我不会说在 PHP 中有一种“正常”的方式来做这样的事情。归根结底,您将变量输出到 HTML 中 - 因此,如果不使用专用模板引擎(和/或转向更加基于 MVC 的方法,如 ryeguy)。

我的建议是:如果不适合您喜欢的风格,请更改它。如果您要开发和维护它,最重要的是您可以舒适、高效地使用它。

Well, it's certainly not an invalid way to do it - but it might be a little cumbersome to read and maintain. I wouldn't say there's a "normal" way to do anything like this in PHP. At the end of the day, you're outputting variables into HTML - so there aren't a variety of options without using a dedicated templating engine (and/or heading towards a more MVC-based approach, as mentioned by ryeguy).

My suggestion: if it doesn't suit your preferred style, change it. The most important thing, if you're going to be developing and maintaining it, is that you can work with it comfortably, and efficiently.

吃→可爱长大的 2024-09-05 12:45:39

有点走在迈向 MVC 的正确轨道上。 MVC 就是关注点分离。这里的想法是,第一个代码段设置值,然后 pageheader.inc 输出它们。这将逻辑排除在视图之外。

做得非常马虎。

It's kind of on the right track towards MVC. MVC is all about separation of concerns. The idea here is that the first snippet sets values, and then pageheader.inc outputs them. This keeps logic out of the view.

It's done very sloppily.

旧时模样 2024-09-05 12:45:39

PHP 是一个非常分散的社区,没有“标准”的做事方式。也就是说,您发布的 pageheader.inc 示例似乎可以简化。顶部的代码对您没有任何好处。仅此一点就足够了:

<html>
<head>
<title><?=@$title?></title>
</head>

如果您正在寻找有关架构和最佳实践的方向,我强烈建议您查看流行的 PHP 框架,例如 Codeigniter 或 Kohana。

PHP is a very fragmented community with no "standard" way of doing things. That said, it appears the pageheader.inc example you posted can be simplified. The code at the top is doing you no good. This alone would suffice:

<html>
<head>
<title><?=@$title?></title>
</head>

If you're looking for some direction on architecture and best practices, I'd highly recommend checking out a popular PHP framework like Codeigniter or Kohana.

混吃等死 2024-09-05 12:45:39

本质上,您可以使用两种方法,它们彼此相反。您有一个方法的示例,其中每个页面都有自己的内容,负责包括页眉、页脚和其他公共数据。这种方法并没有什么特别错误的地方。第二种方法是使用一个页面负责包含页眉、页脚、内容。这是使用流行 PHP 框架的更常见的应用程序。

There are essentially two approaches you can use, and they are inverses of each other. You have an example of one approach, where each page has its own content is responsible for including the header, footer, and other common data. There is nothing particularly wrong with this approach. The second approach is to have a single page that is responsible for including the header, footer, and content. This is far more common applications using popular PHP frameworks.

楠木可依 2024-09-05 12:45:39

我在简单的网站(例如 5-10 页小册子网站)上经常使用这种样式,在这些网站中我需要一些集中代码,例如从几个变量创建菜单和页面标题,但不想要成熟的 MVC 结构,因为它是太过分了。

I've used that style a lot on simple sites like 5-10 page brochure-ware sites where I want some centralised code e.g creating menus and page headers from a few variables, but don't want a full-blown MVC structure as it's be overkill.

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