php-doc-en/faq/using.xml

413 lines
13 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="faq.using" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Using PHP</title>
<titleabbrev>Using PHP</titleabbrev>
<para>
This section gathers many common errors that you may face
while writing PHP scripts.
</para>
<qandaset>
<qandaentry xml:id="faq.using.parameterorder">
<question>
<para>
I cannot remember the parameter order of PHP functions, are they random?
</para>
</question>
<answer>
<para>
PHP is a glue that brings together hundreds of external libraries, so sometimes
this gets messy. However, a simple rule of thumb is as follows:
</para>
<para>
<link linkend="book.array">Array function</link> parameters are ordered
as "<emphasis>needle, haystack</emphasis>" whereas
<link linkend="book.strings">String functions</link> are the opposite,
so "<emphasis>haystack, needle</emphasis>".
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.anyform">
<question>
<para>
I would like to write a generic PHP script that can handle data coming
from any form. How do I know which POST method variables are available?
</para>
</question>
<answer>
<para>
PHP offers many <link linkend="language.variables.predefined">
predefined variables</link>, like the superglobal <varname>
$_POST</varname>. You may loop through <varname>$_POST</varname>
as it's an associate array of all POSTed values. For example, let's
simply loop through it with &foreach;,
check for <function>empty</function> values,
and print them out.
<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">
<!-- TODO Probably should mention not doing this... -->
<question>
<para>
I need to convert all single-quotes (') to a backslash
followed by a single-quote (\'). How can I do this with a
regular expression? I'd also like to convert " to \" and
\ to \\.
</para>
</question>
<answer>
<para>
Assuming this is for a database, use the escaping mechanism that
comes with the database. For example, use
<function>mysql_real_escape_string</function> with MySQL and
<function>pg_escape_string</function> with PostgreSQL. There is
also the generic <function>addslashes</function> and
<function>stripslashes</function> functions, that are more
common with older PHP code.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.wrong-order">
<question>
<para>
When I do the following, the output is printed in
the wrong order:
<programlisting role="php">
<![CDATA[
<?php
function myfunc($argument)
{
echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);
?>
]]>
</programlisting>
what's going on?
</para>
</question>
<answer>
<para>
To be able to use the results of your function in an expression (such
as concatenating it with other strings in the example above), you need
to <function>return</function> the value,
not <function>echo</function> it.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.newlines">
<question>
<para>
Hey, what happened to my newlines?
<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>
In PHP, the ending for a block of code is either "?&gt;" or
"?&gt;\n" (where \n means a newline). So in the example above,
the echoed sentences will be on one line, because PHP omits
the newlines after the block ending. This means that you need to
insert an extra newline after each block of PHP code to make
it print out one newline.
</para>
<para>
Why does PHP do this? Because when formatting normal HTML, this
usually makes your life easier because you don't want that newline,
but you'd have to create extremely long lines or otherwise make the
raw page source unreadable to achieve that effect.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.headers-sent">
<question>
<para>
I get the message 'Warning: Cannot send session cookie - headers already
sent...' or 'Cannot add header information - headers already sent...'.
</para>
</question>
<answer>
<para>
The functions <function>header</function>, <function>setcookie</function>,
and the <link linkend="ref.session">session
functions</link> need to add headers to the output stream but headers
can only be sent before all other content. There can be no output
before using these functions, output such as HTML. The function
<function>headers_sent</function> will check if your script has already
sent headers and see also the <link linkend="ref.outcontrol">Output Control
functions</link>.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.header">
<question>
<para>
I need to access information in the request header directly.
How can I do this?
</para>
</question>
<answer>
<para>
The <function>getallheaders</function> function will do this if
you are running PHP as an Apache module. So, the following bit
of code will show you all the request headers:
<programlisting role="php">
<![CDATA[
<?php
$headers = getallheaders();
foreach ($headers as $name => $content) {
echo "headers[$name] = $content<br />\n";
}
?>
]]>
</programlisting>
</para>
<para>
See also
<function>apache_lookup_uri</function>,
<function>apache_response_headers</function>, and
<function>fsockopen</function>
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.authentication">
<question>
<para>
When I try to use authentication with IIS I get 'No Input file specified'.
</para>
</question>
<answer>
<para>
The security model of IIS is at fault here. This is a problem
common to all CGI programs running under IIS. A workaround is
to create a plain HTML file (not parsed by PHP) as the entry page
into an authenticated directory. Then use a META tag to redirect
to the PHP page, or have a link to the PHP page. PHP will
then recognize the authentication correctly.
This should not affect other
NT web servers. For more information, see:
<link xlink:href="&url.iis;">&url.iis;</link> and the manual
section on <link linkend="features.http-auth">HTTP Authentication
</link>.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.iis.sharing">
<question>
<para>
Windows: I can't access files shared on another computer using IIS
</para>
</question>
<answer>
<para>
You have to change the <literal>Go to Internet Information
Services</literal>. Locate your PHP file and go to its properties.
Go to the <literal>File Security</literal> tab, <literal>Edit -&lt;
Anonymous access and authentication control</literal>.
</para>
<para>
You can fix the problem either by unticking <literal>Anonymous
Access</literal> and leaving <literal>Integrated Window
Authentication</literal> ticked, or, by ticking <literal>Anonymous
Access</literal> and editing the user as he may not have the access
right.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.mixml">
<question>
<para>
How am I supposed to mix XML and PHP? It complains
about my &lt;?xml tags!
</para>
</question>
<answer>
<para>
In order to embed &lt;?xml straight into your PHP code, you'll have to turn off
short tags by having the PHP directive
<link linkend="ini.short-open-tag">short_open_tags</link> set to
<literal>0</literal>. You cannot set this directive with
<function>ini_set</function>. Regardless of
<link linkend="ini.short-open-tag">short_open_tags</link> being on or
off, you can do something like: <literal>&lt;?php echo '&lt;?xml'; ?&gt;</literal>.
The default for this directive is <literal>On</literal>.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.variables">
<question>
<para>
Where can I find a complete list of variables are available to me
in PHP?
</para>
</question>
<answer>
<para>
Read the manual page on <link linkend="language.variables.predefined">
predefined variables</link> as it includes a partial list of predefined
variables available to your script. A complete list of available
variables (and much more information) can be seen by calling the
<function>phpinfo</function> function. Be sure to read the manual
section on <link linkend="language.variables.external">variables from
outside of PHP</link> as it describes common scenarios for
external variables, like from a HTML form, a Cookie, and the URL.
</para>
&note.registerglobals;
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.freepdf">
<question>
<para>
How can I generate PDF files without using the non-free and
commercial libraries like
PDFLib? I'd like something that's
free and doesn't require external PDF libraries.
</para>
</question>
<answer>
<para>
There are a few alternatives written in PHP such as
<link xlink:href="&url.pdf.fpdf;">FPDF</link> and
<link xlink:href="&url.pdf.tcpdf;">TCPDF</link>.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.cgi-vars">
<question>
<para>
I'm trying to access one of the standard CGI
variables (such as <varname>$DOCUMENT_ROOT</varname> or
<varname>$HTTP_REFERER</varname>) in a user-defined
function, and it can't seem to find it. What's wrong?
</para>
</question>
<answer>
<para>
Use <varname>$_SERVER['DOCUMENT_ROOT']</varname> and
<varname>$_SERVER['HTTP_REFERER']</varname> instead.
</para>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.shorthandbytes">
<question>
<para>
A few PHP directives may also take on shorthand byte values, as opposed
to only <type>int</type> byte values. What are all the available
shorthand byte options?
</para>
</question>
<answer>
<para>
The available options are K (for Kilobytes), M (for Megabytes) and G (for
Gigabytes; available since PHP 5.1.0), and are all case-insensitive.
Anything else assumes bytes. <literal>1M</literal> equals one Megabyte or
<literal>1048576</literal> bytes. <literal>1K</literal> equals one
Kilobyte or <literal>1024</literal> bytes. These shorthand notations may
be used in &php.ini; and in the <function>ini_set</function> function.
Note that the numeric value is cast to <type>int</type>;
for instance, <literal>0.5M</literal> is interpreted as <literal>0</literal>.
</para>
<note>
<title>kilobyte versus kibibyte</title>
<para>
The PHP notation describes one kilobyte as equalling 1024 bytes, whereas
the <acronym>IEC</acronym> standard considers this to be a kibibyte instead.
Summary: k and K = 1024 bytes.
</para>
</note>
</answer>
</qandaentry>
<qandaentry xml:id="faq.using.windowslocalhostissue">
<question>
<para>
Windows: I keep getting connection timeouts when using <literal>localhost</literal>,
whereas <literal>"127.0.0.1"</literal> works?
</para>
</question>
<answer>
<para>
Prior to PHP 5.3.4, there was a bug in the network resolving code inside PHP that
caused <literal>localhost</literal> in all stream related situations to fail if IPv6
was enabled. To work around this issue you can either use <literal>"127.0.0.1"</literal>
or disable IPv6 resolving in the <filename>hosts</filename> file.
</para>
</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
-->