<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/faq.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'faq.using.php',
    1 => 'Using PHP',
    2 => 'Using PHP',
  ),
  'up' => 
  array (
    0 => 'faq.php',
    1 => 'FAQ',
  ),
  'prev' => 
  array (
    0 => 'faq.build.php',
    1 => 'Build Problems',
  ),
  'next' => 
  array (
    0 => 'faq.passwords.php',
    1 => 'Password Hashing',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'faq/using.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="faq.using" class="chapter">
  <h1 class="title">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.parameterorder">
     
      I cannot remember the parameter order of PHP functions, are they random?
     
    </a></li><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 (&#039;) to a backslash 
      followed by a single-quote (\&#039;). How can I do this with a 
      regular expression?  I&#039;d also like to convert &quot; to \&quot; and
      \ to \\.
     
    </a></li><li><a href="#faq.using.wrong-order">
     
      When I do the following, the output is printed in 
      the wrong order: 
     

&lt;?php
function myfunc($argument)
{
    echo $argument + 10;
}
$variable = 10;
echo &quot;myfunc($variable) = &quot; . myfunc($variable);
?&gt;

    
     what&#039;s going on?
     
    </a></li><li><a href="#faq.using.newlines">
     
      Hey, what happened to my newlines?
      

&lt;pre&gt;
&lt;?php echo &quot;This should be the first line.&quot;; ?&gt;
&lt;?php echo &quot;This should show up after the new line above.&quot;; ?&gt;
&lt;/pre&gt;

      
     
    </a></li><li><a href="#faq.using.headers-sent">
     
      I get the message &#039;Warning: Cannot send session cookie - headers already
      sent...&#039; or &#039;Cannot add header information - headers already sent...&#039;.
     
    </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 &#039;No Input file specified&#039;.
     
    </a></li><li><a href="#faq.using.iis.sharing">
     
      Windows: I can&#039;t access files shared on another computer using IIS
     
    </a></li><li><a href="#faq.using.mixml">
     
      How am I supposed to mix XML and PHP? It complains 
      about my &lt;?xml tags!
     
    </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&#039;d like something that&#039;s 
      free and doesn&#039;t require external PDF libraries.
     
    </a></li><li><a href="#faq.using.shorthandbytes">
     
      A few PHP directives may also take on shorthand byte values, as opposed
      to only int byte values.  What are all the available
      shorthand byte options?
     
    </a></li></ol></div>

   <dl class="qandaentry" id="faq.using.parameterorder">
    <dt><strong>
     
      I cannot remember the parameter order of PHP functions, are they random?
     
    </strong></dt>
    <dd class="answer">
     <p class="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: 
     </p>
     <p class="para">
      <a href="book.array.php" class="link">Array function</a> parameters are ordered
      as &quot;<em>needle, haystack</em>&quot; whereas 
      <a href="book.strings.php" class="link">String functions</a> are the opposite,
      so &quot;<em>haystack, needle</em>&quot;.
     </p>
     <p class="para">
      As of PHP 8.0, <a href="functions.arguments.php#functions.named-arguments" class="link">named arguments</a>
      allow passing arguments by parameter name, making parameter order less of a concern.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.anyform">
    <dt><strong>
     
      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?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      PHP offers many <a href="language.variables.predefined.php" class="link">
      predefined variables</a>, like the superglobal <var class="varname"><a href="reserved.variables.post.php" class="classname">$_POST</a></var>.
      You may loop through <var class="varname"><a href="reserved.variables.post.php" class="classname">$_POST</a></var>
      as it&#039;s an associate array of all POSTed values.  For example, let&#039;s
      simply loop through it with <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a>, 
      check for <span class="function"><a href="function.empty.php" class="function">empty()</a></span> values,
      and print them out.
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">=&gt; </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">"&lt;pre&gt;"</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">?&gt;</span></span></code></div>
      </div>

     </p>

    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.addslashes">
    <dt><strong>
     
      I need to convert all single-quotes (&#039;) to a backslash 
      followed by a single-quote (\&#039;). How can I do this with a 
      regular expression?  I&#039;d also like to convert &quot; to \&quot; and
      \ to \\.
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      Assuming this is for a database, use the escaping mechanism that
      comes with the database. For example, use 
      <span class="function"><a href="function.mysql-real-escape-string.php" class="function">mysql_real_escape_string()</a></span> with MySQL and
      <span class="function"><a href="function.pg-escape-string.php" class="function">pg_escape_string()</a></span> with PostgreSQL. There is
      also the generic <span class="function"><a href="function.addslashes.php" class="function">addslashes()</a></span> and
      <span class="function"><a href="function.stripslashes.php" class="function">stripslashes()</a></span> functions, that are more
      common with older PHP code.
     </p>
     <p class="para">
      Manually escaping values is error-prone and depends on context.
      Prefer using database APIs that support prepared statements and
      parameter binding instead of constructing queries by concatenating
      escaped strings.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.wrong-order">
    <dt><strong>
     
      When I do the following, the output is printed in 
      the wrong order: 
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">myfunc</span><span style="color: #007700">(</span><span style="color: #0000BB">$argument</span><span style="color: #007700">)<br />{<br />    echo </span><span style="color: #0000BB">$argument </span><span style="color: #007700">+ </span><span style="color: #0000BB">10</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$variable </span><span style="color: #007700">= </span><span style="color: #0000BB">10</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"myfunc(</span><span style="color: #0000BB">$variable</span><span style="color: #DD0000">) = " </span><span style="color: #007700">. </span><span style="color: #0000BB">myfunc</span><span style="color: #007700">(</span><span style="color: #0000BB">$variable</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

     what&#039;s going on?
     
    </strong></dt>
    <dd class="answer">
     <p class="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 <span class="function"><a href="function.return.php" class="function">return</a></span> the value, 
      not <span class="function"><a href="function.echo.php" class="function">echo</a></span> it.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.newlines">
    <dt><strong>
     
      Hey, what happened to my newlines?
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">&lt;pre&gt;<br /><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">"This should be the first line."</span><span style="color: #007700">; </span><span style="color: #0000BB">?&gt;<br />&lt;?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">"This should show up after the new line above."</span><span style="color: #007700">; </span><span style="color: #0000BB">?&gt;<br /></span>&lt;/pre&gt;</span></code></div>
      </div>

     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      In PHP, the ending for a block of code is either &quot;?&gt;&quot; or
      &quot;?&gt;\n&quot; (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.
     </p>
     <p class="para">
      Why does PHP do this? Because when formatting normal HTML, this
      usually makes your life easier because you don&#039;t want that newline,
      but you&#039;d have to create extremely long lines or otherwise make the
      raw page source unreadable to achieve that effect.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.headers-sent">
    <dt><strong>
     
      I get the message &#039;Warning: Cannot send session cookie - headers already
      sent...&#039; or &#039;Cannot add header information - headers already sent...&#039;.
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      The functions <span class="function"><a href="function.header.php" class="function">header()</a></span>, <span class="function"><a href="function.setcookie.php" class="function">setcookie()</a></span>,
      and the <a href="ref.session.php" class="link">session 
      functions</a> 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 
      <span class="function"><a href="function.headers-sent.php" class="function">headers_sent()</a></span> will check if your script has already 
      sent headers and see also the <a href="ref.outcontrol.php" class="link">Output Control
      functions</a>.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.header">
    <dt><strong>
     
      I need to access information in the request header directly. 
      How can I do this?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      The <span class="function"><a href="function.getallheaders.php" class="function">getallheaders()</a></span> 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:
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$headers </span><span style="color: #007700">= </span><span style="color: #0000BB">getallheaders</span><span style="color: #007700">();<br />foreach (</span><span style="color: #0000BB">$headers </span><span style="color: #007700">as </span><span style="color: #0000BB">$name </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$content</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"headers[</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">] = </span><span style="color: #0000BB">$content</span><span style="color: #DD0000">&lt;br /&gt;\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </p>
     <p class="para">
      See also 
      <span class="function"><a href="function.apache-lookup-uri.php" class="function">apache_lookup_uri()</a></span>,
      <span class="function"><a href="function.apache-response-headers.php" class="function">apache_response_headers()</a></span>, and
      <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span>
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.authentication">
    <dt><strong>
     
      When I try to use authentication with IIS I get &#039;No Input file specified&#039;.
     
    </strong></dt>
    <dd class="answer">
     <p class="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: 
      <a href="http://support.microsoft.com/kb/q160422/" class="link external">&raquo;&nbsp;http://support.microsoft.com/kb/q160422/</a> and the manual
      section on <a href="features.http-auth.php" class="link">HTTP Authentication
      </a>.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.iis.sharing">
    <dt><strong>
     
      Windows: I can&#039;t access files shared on another computer using IIS
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      You have to change the <code class="literal">Go to Internet Information
      Services</code>. Locate your PHP file and go to its properties.
      Go to the <code class="literal">File Security</code> tab, <code class="literal">Edit -&lt; 
      Anonymous access and authentication control</code>.
     </p>
     <p class="para">
      You can fix the problem either by unticking <code class="literal">Anonymous
      Access</code> and leaving <code class="literal">Integrated Window
      Authentication</code> ticked, or, by ticking <code class="literal">Anonymous
      Access</code> and editing the user as he may not have the access
      right. 
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.mixml">
    <dt><strong>
     
      How am I supposed to mix XML and PHP? It complains 
      about my &lt;?xml tags!
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      In order to embed &lt;?xml straight into your PHP code, you&#039;ll have to turn off
      short tags by having the PHP directive 
      <a href="ini.core.php#ini.short-open-tag" class="link">short_open_tags</a> set to 
      <code class="literal">0</code>.  You cannot set this directive with 
      <span class="function"><a href="function.ini-set.php" class="function">ini_set()</a></span>.  Regardless of 
      <a href="ini.core.php#ini.short-open-tag" class="link">short_open_tags</a> being on or 
      off, you can do something like: <code class="literal">&lt;?php echo &#039;&lt;?xml&#039;; ?&gt;</code>.  
      The default for this directive is <code class="literal">On</code>.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.variables">
    <dt><strong>
     
      Where can I find a complete list of variables are available to me 
      in PHP?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      Read the manual page on <a href="language.variables.predefined.php" class="link">
      predefined variables</a> 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 
      <span class="function"><a href="function.phpinfo.php" class="function">phpinfo()</a></span> function.  Be sure to read the manual 
      section on <a href="language.variables.external.php" class="link">variables from 
      outside of PHP</a> as it describes common scenarios for 
      external variables, like from a HTML form, a Cookie, and the URL.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.freepdf">
    <dt><strong>
     
      How can I generate PDF files without using the non-free and 
      commercial libraries like 
      PDFLib?  I&#039;d like something that&#039;s 
      free and doesn&#039;t require external PDF libraries.
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      There are a few alternatives written in PHP such as 
      <a href="http://www.fpdf.org/" class="link external">&raquo;&nbsp;FPDF</a> and
      <a href="http://www.tcpdf.org/" class="link external">&raquo;&nbsp;TCPDF</a>.
     </p>
    </dd>
   </dl>

   <dl class="qandaentry" id="faq.using.shorthandbytes">
    <dt><strong>
     
      A few PHP directives may also take on shorthand byte values, as opposed
      to only <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> byte values.  What are all the available
      shorthand byte options?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      The available options are K (for Kilobytes), M (for Megabytes) and G (for
      Gigabytes), and are all case-insensitive.
      Anything else assumes bytes. <code class="literal">1M</code> equals one Megabyte or
      <code class="literal">1048576</code> bytes. <code class="literal">1K</code> equals one
      Kilobyte or <code class="literal">1024</code> bytes. These shorthand notations may
      be used in <var class="filename">php.ini</var> and in the <span class="function"><a href="function.ini-set.php" class="function">ini_set()</a></span> function.
      Note that the numeric value is cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>;
      for instance, <code class="literal">0.5M</code> is interpreted as <code class="literal">0</code>.
     </p>
     <blockquote class="note"><p><strong class="note">Note</strong>: 
      <strong>kilobyte versus kibibyte</strong><br />
      <p class="para">
       The PHP notation describes one kilobyte as equalling 1024 bytes, whereas
       the <abbr>IEC</abbr> standard considers this to be a kibibyte instead.
       Summary: k and K = 1024 bytes.
      </p>
     </p></blockquote>
    </dd>
   </dl>
  
 </div>
<?php manual_footer($setup); ?>