📄 faq.using.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Using PHP</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="faq.build.html">Build Problems</a></div> <div class="next" style="text-align: right; float: right;"><a href="faq.html.html">PHP and HTML</a></div> <div class="up"><a href="faq.html">FAQ</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Using PHP</h1> <p class="para"> This section gathers many common errors that you may face while writing PHP scripts. </p> <div class="qandaset"><ol class="qandaset_questions"><li><a href="#faq.using.anyform"> 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? </a></li><li><a href="#faq.using.addslashes"> 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 \\. </a></li><li><a href="#faq.using.stripslashes"> All my " turn into \" and my ' turn into \', how do I get rid of all these unwanted backslashes? How and why did they get there? </a></li><li><a href="#faq.register-globals"> How does the PHP directive register_globals affect me? </a></li><li><a href="#faq.using.wrong-order"> When I do the following, the output is printed in the wrong order: <?phpfunction myfunc($argument){ echo $argument + 10;}$variable = 10;echo "myfunc($variable) = " . myfunc($variable);?> what's going on? </a></li><li><a href="#faq.using.newlines"> Hey, what happened to my newlines? <pre><?php echo "This should be the first line."; ?><?php echo "This should show up after the new line above."; ?></pre> </a></li><li><a href="#faq.using.headers-sent"> I get the message 'Warning: Cannot send session cookie - headers already sent...' or 'Cannot add header information - headers already sent...'. </a></li><li><a href="#faq.using.header"> I need to access information in the request header directly. How can I do this? </a></li><li><a href="#faq.using.authentication"> When I try to use authentication with IIS I get 'No Input file specified'. </a></li><li><a href="#faq.using.iis.sharing"> Windows: I can't access files shared on another computer using IIS </a></li><li><a href="#faq.using.netscape"> My PHP script works on IE and Lynx, but on Netscape some of my output is missing. When I do a "View Source" I see the content in IE but not in Netscape. </a></li><li><a href="#faq.using.mixml"> How am I supposed to mix XML and PHP? It complains about my <?xml tags! </a></li><li><a href="#faq.using.editor"> How can I use PHP with FrontPage or some other HTML editor that insists on moving my code around? </a></li><li><a href="#faq.using.variables"> Where can I find a complete list of variables are available to me in PHP? </a></li><li><a href="#faq.using.freepdf"> 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. </a></li><li><a href="#faq.using.cgi-vars"> I'm trying to access one of the standard CGI variables (such as $DOCUMENT_ROOT or $HTTP_REFERER) in a user-defined function, and it can't seem to find it. What's wrong? </a></li><li><a href="#faq.using.shorthandbytes"> A few PHP directives may also take on shorthand byte values, as opposed to only integer byte values. What are all the available shorthand byte options? And can I use these outside of php.ini? </a></li></ol> <dl> <dt><strong> <p class="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? </p> </strong></dt> <dd><a name="faq.using.anyform"></a> <p class="para"> PHP offers many <a href="language.variables.predefined.html" class="link"> predefined variables</a>, like the superglobal <var class="varname"> $_POST</var>. You may loop through <var class="varname"><a href="reserved.variables.post.html" class="classname">$_POST</a></var> as it's an associate array of all POSTed values. For example, let's simply loop through it with <a href="control-structures.foreach.html" class="link"> foreach</a>, check for <a href="function.empty.html" class="function">empty()</a> values, and print them out. <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$empty </span><span style="color: #007700">= </span><span style="color: #0000BB">$post </span><span style="color: #007700">= array();<br />foreach (</span><span style="color: #0000BB">$_POST </span><span style="color: #007700">as </span><span style="color: #0000BB">$varname </span><span style="color: #007700">=> </span><span style="color: #0000BB">$varvalue</span><span style="color: #007700">) {<br /> if (empty(</span><span style="color: #0000BB">$varvalue</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$empty</span><span style="color: #007700">[</span><span style="color: #0000BB">$varname</span><span style="color: #007700">] = </span><span style="color: #0000BB">$varvalue</span><span style="color: #007700">;<br /> } else {<br /> </span><span style="color: #0000BB">$post</span><span style="color: #007700">[</span><span style="color: #0000BB">$varname</span><span style="color: #007700">] = </span><span style="color: #0000BB">$varvalue</span><span style="color: #007700">;<br /> }<br />}<br /><br />print </span><span style="color: #DD0000">"<pre>"</span><span style="color: #007700">;<br />if (empty(</span><span style="color: #0000BB">$empty</span><span style="color: #007700">)) {<br /> print </span><span style="color: #DD0000">"None of the POSTed values are empty, posted:\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">);<br />} else {<br /> print </span><span style="color: #DD0000">"We have " </span><span style="color: #007700">. </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$empty</span><span style="color: #007700">) . </span><span style="color: #DD0000">" empty values\n"</span><span style="color: #007700">;<br /> print </span><span style="color: #DD0000">"Posted:\n"</span><span style="color: #007700">; </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">);<br /> print </span><span style="color: #DD0000">"Empty:\n"</span><span style="color: #007700">; </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$empty</span><span style="color: #007700">);<br /> exit;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </p> <blockquote><p><b class="note">Note</b>: <b>Superglobals: availability note</b><br />Superglobal arrays such as <var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET</a></var>,<var class="varname"><a href="reserved.variables.post.html" class="classname">$_POST</a></var>, and <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var>, etc. are availableas of PHP 4.1.0. For more information, read the manual section on<a href="language.variables.predefined.html" class="link">superglobals</a><br /></p></blockquote> </dd> </dl> <dl> <dt><strong> <p class="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 \\. </p> </strong></dt> <dd><a name="faq.using.addslashes"></a> <p class="para"> The function <a href="function.addslashes.html" class="function">addslashes()</a> will do this. See also <a href="function.mysql-escape-string.html" class="function">mysql_escape_string()</a>. You may also strip backslashes with <a href="function.stripslashes.html" class="function">stripslashes()</a>. </p> <blockquote><p><b class="note">Note</b>: <b>directive note: magic_quotes_gpc</b><br />The <a href="info.configuration.html#ini.magic-quotes-gpc" class="link">magic_quotes_gpc</a>directive defaults to <i>on</i>. It essentially runs<a href="function.addslashes.html" class="function">addslashes()</a> on all GET, POST, and COOKIE data.<a href="function.stripslashes.html" class="function">stripslashes()</a> may be used to remove them.<br /></p></blockquote> </dd> </dl> <dl> <dt><strong> <p class="para"> All my " turn into \" and my ' turn into \', how do I get rid of all these unwanted backslashes? How and why did they get there? </p> </strong></dt> <dd><a name="faq.using.stripslashes"></a> <p class="para"> The PHP function <a href="function.stripslashes.html" class="function">stripslashes()</a> will strip those backslashes from your <a href="language.types.string.html" class="type string">string</a>. Most likely the backslashes magically exist because the PHP directive <a href="info.configuration.html#ini.magic-quotes-gpc" class="link">magic_quotes_gpc</a> is on. </p> <blockquote><p><b class="note">Note</b>: <b>directive note: magic_quotes_gpc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -