1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
archived-doc-zh/faq/using.xml
Louis-Arnaud ecf1e5b1cc Check translation for faq (#1016)
* Fix faq: critical translation errors and Chinese typos

- using.xml: 区分大小写→不区分大小写 (opposite meaning), update shorthand notation usage
- mailinglist.xml: remove stray 'å' character, <literal>→<emphasis> for emphasis
- com.xml: fix &amp;true; → &true; entity references, <xref>→<link> to match EN
- obtaining.xml: 组和→组合 typo
- passwords.xml: 将会窃取→将被窃取 (missing passive voice)
- html.xml: add missing <example><title> wrapper for Javascript example

* Fix faq/installation.xml: sync with EN, remove outdated content

- Remove obsolete MacOS parenthetical
- Remove extra Windows threading paragraph
- php.ini-dist → php.ini-development
- ereg → preg_match (ereg removed in PHP 7)
- php4 → php5 in paths, php4ts.dll → php5ts.dll
- Add missing php_ini_loaded_file reference
- Remove Win98/Me autoexec.bat section
- Remove obsolete Windows system directory paragraph
- Simplify Windows PATH instructions to match EN
- Remove PHP 4 handler from multiviews example

* Fix faq/com.xml: replace Unicode smart quotes with ASCII quotes

The linkend attributes used Unicode right double quotation marks (U+201D)
instead of ASCII double quotes, causing XML parse failures in CI.

* Update com.xml

将中文句式中的英文引号修改为中文引号

---------

Co-authored-by: mowangjuanzi <mowangjuanzi@petalmail.com>
2026-03-17 18:02:00 +08:00

369 lines
13 KiB
XML
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: aab33d644359aba597e810e2fc0c0caa0d347c9c Maintainer: Avenger Status: ready -->
<chapter xml:id="faq.using"
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink">
<title>使用 PHP</title>
<titleabbrev>使用 PHP</titleabbrev>
<para>本节汇集了你在写 PHP 脚本时可能碰到的大多数普通错误。</para>
<qandaset>
<qandaentry xml:id="faq.using.parameterorder">
<!-- TODO: Mention named arguments -->
<question>
<para>
我忘了 PHP 函数的参数顺序,它们是随机的吗?
</para>
</question>
<answer>
<para>
PHP 是一个将数百个外部库连接在一起的粘合剂,尽管有时候会有一些混乱。这里有一些简单的经验法则:
</para>
<para>
<link linkend="book.array">数组函数</link> 参数的顺序为
"<emphasis>needle, haystack</emphasis>"
<link linkend="book.strings">字符串函数</link> 则相反,顺序为
"<emphasis>haystack, needle</emphasis>"。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.anyform">
<question>
<para>
我想写一个可以处理任何表单来的数据的通用 PHP
脚本。我怎么知道哪个 POST 方法变量可用呢?
</para>
</question>
<answer>
<para>
PHP 提供很多“
<link linkend="language.variables.predefined">预定义变量</link>”,例如超全局变量
<varname>$_POST</varname>。可以遍历
<varname>$_POST</varname>变量,因为它是一个和所有通过 POST 方法传递数据相联系的数组。例如,可以用
&foreach;简单地遍历它,检查
<function>empty</function>值,以及将它们输出。
<programlisting role="php">
<![CDATA[
<?php
$empty = $post = array();
foreach ($_POST as $varname => $varvalue) {
if (empty($varvalue)) {
$empty[$varname] = $varvalue;
} else {
$post[$varname] = $varvalue;
}
}
print "<pre>";
if (empty($empty)) {
print "None of the POSTed values are empty, posted:\n";
var_dump($post);
} else {
print "We have " . count($empty) . " empty values\n";
print "Posted:\n"; var_dump($post);
print "Empty:\n"; var_dump($empty);
exit;
}
?>
]]>
</programlisting>
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.addslashes">
<question>
<para>
我需要在所有的单引号(')前加一个反斜线(\),使它们变成(\')。我如何能够通过正则表达式来实现?
我同样希望能够将 " 转换成 \",将 \ 转换成 \\。
</para>
</question>
<answer>
<para>
假设是针对数据库,请使用数据库自带的转义机制。比如,针对 MySQL 使用
<function>mysql_real_escape_string</function> 函数,针对 PostgreSQL 则使用
<function>pg_escape_string</function> 函数。还有两个标准函数
<function>addslashes</function>
<function>stripslashes</function> 可以在旧的 PHP 版本中实现类似的操作。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.wrong-order">
<question>
<para>
当我这样做时,输出显示的次序是错的:
<programlisting role="php">
<![CDATA[
<?php
function myfunc($argument)
{
echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);
?>
]]>
</programlisting>
这是怎么回事?
</para>
</question>
<answer>
<para>
要在一个表达式中(例如在上面的例子中和其它字符串连接)使用函数的结果,需要
<function>return</function> 这个值,而不是
<function>echo</function> 它。</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.newlines">
<question>
<para>
下面代码怎么没有分成两行显示?
<programlisting role="php">
<![CDATA[
<pre>
<?php echo "This should be the first line."; ?>
<?php echo "This should show up after the new line above."; ?>
</pre>
]]>
</programlisting>
</para>
</question>
<answer>
<para>
在 PHP 中,一段代码的结束标记要么是“?&gt;”要么是“?&gt;\n”\n 表示换行)。因此在上面的例子中,输出的句子将显示在同一行中,因为
PHP 忽略了代码结束标记后面的换行。这意味着如果要输出一个换行符,需要在每段
PHP 代码的结束标记后面多加一个换行。
</para>
<para>
PHP 为什么这么做呢?因为在格式化正常的
HTML 时,这样通常会更容易。假如输出了换行而你不需要这个换行时,就不得不用一个非常长的行来达到这样的效果,或者让产生的
HTML 页面的源文件的格式很难读。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.headers-sent">
<question>
<para>
我得到消息 “Warning: Cannot send session cookie - headers already sent...”
或者 “Cannot add header information - headers already sent...”。
</para>
</question>
<answer>
<para>
函数
<function>header</function>
<function>setcookie</function>
<link linkend="ref.session">session 函数</link>需要在输出流中添加头信息。但是头信息只能在其它任何输出内容之前发送。在使用这些函数前不能有任何(如 HTML的输出。函数
<function>headers_sent</function> 能够检查脚本是否已经发送了头信息。请参阅
<link linkend="ref.outcontrol">输出控制函数</link>
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.header">
<question>
<para>
我需要直接访问请求报头中的信息,怎么能办到?
</para>
</question>
<answer>
<para>
如果以 Apache 的模块方式运行 PHP那么函数
<function>getallheaders</function> 可以做这件事。因此下面的代码可以显示所有的请求报头:
<programlisting role="php">
<![CDATA[
<?php
$headers = getallheaders();
foreach ($headers as $name => $content) {
echo "headers[$name] = $content<br />\n";
}
?>
]]>
</programlisting>
</para>
<para>
请参阅函数
<function>apache_lookup_uri</function>
<function>apache_response_headers</function>
<function>fsockopen</function>
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.authentication">
<question>
<para>
当我用 IIS 进行 HTTP 认证时得到 “No Input file specified” 消息。
</para>
</question>
<answer>
<para>
IIS 的安全模型这里有毛病。这是所有 IIS 下运行的 CGI 程序所共有的问题。一个解决办法是建立一个纯
HTML 文件(不经过 PHP 解析)作为要进入认证目录的登录页面,然后用 META 标记来重定向到 PHP 页面,或者用一个连接到
PHP 页面。然后 PHP 就可以正确识别认证信息了。如果是用 ISAPI 模块,那没有这个问题。其它 NT 下的 web 服务器不受此影响。更多信息见
<link xlink:href="&url.iis;">&url.iis;</link>
<link linkend="features.http-auth">HTTP 认证</link> 一章。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.iis.sharing">
<question>
<para>
Windows不能访问另一台电脑上用 IIS 共享的文件。
</para>
</question>
<answer>
<para>
必须要做些修改。打开
<literal>Internet 信息服务</literal>,找到你的 PHP 文件并打开属性页,选择
<literal>文件安全性</literal>标签,点击
<literal>匿名访问和身份验证控制</literal>的“编辑”按钮。
</para>
<para>
解决此问题有两个方法,一是不要选中
<literal>匿名访问</literal>并且选中
<literal>集成 Windows 身份验证</literal>,二是选中
<literal>匿名访问</literal>并编辑匿名访问使用的帐户,改成一个有权限的。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.mixml">
<question>
<para>
我怎样混合使用 XML 和 PHP它不认我的 &lt;?xml 标记!
</para>
</question>
<answer>
<para>
要能够在 PHP 代码中直接嵌入 &lt;?xml需要将 PHP 设置项
<link linkend="ini.short-open-tag">short_open_tags</link>设置为
<literal>0</literal>以关闭短标记格式。无法通过函数
<function>ini_set</function>来更改这项设置。不管
<link linkend="ini.short-open-tag">short_open_tags</link>是开或者关,都可以用类似于
<literal>&lt;?php echo &#39;&lt;?xml&#39;; ?&gt;</literal>的方法达到目的。该项设置的默认值为开。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.variables">
<question>
<para>
哪里可以找到所有可用的 PHP 预定义变量的完整列表?
</para>
</question>
<answer>
<para>
请阅读手册中的
<link linkend="language.variables.predefined">预定义变量</link>
一章,该部分的文档已经包含了一部分可以用于你的脚本的预定义变量的列表。可用变量的完整列表(及更多信息)可以通过调用
<function>phpinfo</function>
函数来查阅。请务必阅读手册中
<link linkend="language.variables.external">来自 PHP 之外的变量</link>
一节,这部分文档阐述了外部变量的概念,例如来自 HTML 表单、Cookie 和 URL 的变量。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.freepdf">
<question>
<para>
怎样才能不用非免费的商业库(例如 PDFLib 来生成 PDF 文档?我想要个免费的并且不需要再连接别的 PDF 库。
</para>
</question>
<answer>
<para>有几个选择,例如
<link xlink:href="&url.pdf.fpdf;">FPDF</link>
<link xlink:href="&url.pdf.tcpdf;">TCPDF</link> 模块。
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.shorthandbytes">
<question>
<para>
有些 PHP 选项可以接受缩写的字节值,与仅能接受
<type>int</type> 字节值相反。都有哪些缩写字节值?
</para>
</question>
<answer>
<para>
可用的选择有 K对应 KilobytesM对应 Megabytes和 G对应 Gigabytes不区分大小写。其余的都认为是字节。
<literal>1M</literal>等于一个 Megabyte
<literal>1048576</literal> 字节。
<literal>1K</literal> 等于一个 Kilobyte
<literal>1024</literal> 字节。这些缩写符号可以在 &php.ini;
<function>ini_set</function> 函数中使用。要注意,数字类型为
<type>int</type> 会自动取整,这意味着
<literal>0.5M</literal><literal>0</literal> 是等价的。
</para>
<note>
<title>kilobyte 和 kibibyte 的区别</title>
<para>
PHP 将一个千字节kilobyte描述为等于 1024 字节bytes
<acronym>IEC</acronym> 标准则认为这是一个 kibibyte。结论k 和 K = 1024 bytes.
</para>
</note>
</answer>
</qandaentry>
</qandaset>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->