PHP 语法“
发布于 2024-12-03 17:18:36 字数 500 浏览 2 评论 0 原文

阅读一些 PHP 代码,我无法理解以下内容的用法:

我理解 php 标签的用法:

我正在阅读的确切用法是:

hidden('mode',$mode); ?>

其中 $form 是对象的新实例化类并“隐藏”了一个类方法。所有这些都可以理解,但是当我在 ? 之间放置一个空格时?和 = 我收到错误:

Parse error: syntax error, unexpected '=' in /home/...

我无法在 google 或 php.net 上找到有关语法的任何内容。

任何帮助将不胜感激。

Reading some PHP code and I cannot understand the usage of:

<?=

I understand the php tag use of:

<?php and <?

The exact usage I am reading is:

<?=$form->hidden('mode',$mode); ?>

where $form is a new instantiated class of an object and "hidden" a class method. All of that is understood, but when I place even a space between the ? and = of <?= I get an error of:

Parse error: syntax error, unexpected '=' in /home/...

I cannot find anything on google or php.net regarding the syntax.

Any help would be appreciated.

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

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

发布评论

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

评论(8

2024-12-10 17:18:36

这是 echo 的快捷方式。像这样的东西(其中expr是任何表达式):

<?= expr ?>

在功能上等同于:

<?php echo expr; ?>

不建议使用此语法,因为它在某些服务器上未启用。 (如果有人使用共享主机,他们也可能无法重新启用它)

它的 PHP 文档是 此处

It's a shortcut for echo. Something like this (where expr is any expression):

<?= expr ?>

is functionally equivalent to this:

<?php echo expr; ?>

It's not recommended to use this syntax as it's not enabled on some servers. (and if someone is using shared hosting, they may not be able to re-enable it, either)

The PHP documentation on it is here.

千仐 2024-12-10 17:18:36

的简写。不再推荐使用它,显然是因为解析器存在一些歧义或与某些 PHP 版本不兼容,尽管我仍然在像 Drupal 这样的大型项目中看到它。

编辑:它们被称为“短标签”,关于是否应该使用它们的讨论可以在这里找到:PHP 短标签可以使用吗?

<?= is shorthand for <?php echo. Its usage isn't recommended any more, apparently because of some ambiguity with the parser or incompatibility with some PHP versions, though I still see it in big projects like Drupal.

Edit: They're called "short tags" and a discussion on whether they should be used or not can be found here: Are PHP short tags acceptable to use?

诺曦 2024-12-10 17:18:36

简而言之,“短开放标签”简直就是地狱。如果您的 php.ini / httpd.conf / .htaccess 中启用了 Short_open_tags,并且您尝试将 XML 标记放入您的文档中,就像这样...

<?xml version="1.0" encoding="iso-8859-1"?>

PHP 会认为您尝试编写一些 PHP,然后出错,然后死掉。你最终不得不做

<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>'; ?>

"Short open tags" are hell, in short. If you have short_open_tags on in your php.ini / httpd.conf / .htaccess and you try to put an XML tag into your document, like so...

<?xml version="1.0" encoding="iso-8859-1"?>

PHP will think you tried to write some PHP, error, and die. You end up having to do

<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>'; ?>
℡Ms空城旧梦 2024-12-10 17:18:36

它称为短开放标记。如果您的服务器上启用了它,那么您就可以使用它。

来自 PHP 手册:

该指令还影响了 PHP 5.4.0 之前的简写
始终可用。


使用这些类型的标签可以让您收到与使用 echo 相同的结果。以下示例是等效的。

示例:

<?php echo $text; ?>
<?= $text; ?>

It is called the short open tag. If it is enabled on your server, then you may use it.

From PHP Manual:

This directive also affected the shorthand <?= before PHP 5.4.0, which
is identical to <? echo. Use of this shortcut required short_open_tag
to be on. Since PHP 5.4.0, <?= is always available.

The use of these type of tags allow you to receive the same result as using echo. The following examples are equivalent.

Example:

<?php echo $text; ?>
<?= $text; ?>
娇柔作态 2024-12-10 17:18:36

,当启用 short_tags 时,是 的简写。

在 PHP 手册的开头对此进行了介绍。

<?=, when short_tags is enabled, is a shorthand for <?php echo.

This is covered right at the start of the PHP manual.

花伊自在美 2024-12-10 17:18:36

例如,当您在 HTML 文件中时,只是快速打印内容的简写。不建议使用它,因为它已被弃用。

Just a shorthand to quickly print content when, for example, you are in an HTML file. Its usage is not recommended since it is deprecated.

忆离笙 2024-12-10 17:18:36

这是一个短标签,并非可在所有服务器上使用。

相同

中放置空格会使短标签无效,这就是为什么你得到你的错误。

That is a short tag and is not available for use on all servers.

<?= is the same as <?php echo

Putting a space in <?= invalidates the short tag which is why you get your error.

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