Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several ways to achieve this.</p> <h2>Use relative path from <code>form.php</code><br></h2> <pre><code>require_once __DIR__ . '/otherfile.php'; </code></pre> <p>If you're using PHP 5.2 or older, you can use <code>dirname(__FILE__)</code> instead of <code>__DIR__</code>. Read more about magic constants <a href="http://php.net/manual/en/language.constants.predefined.php" rel="noreferrer">here</a>.</p> <h2>Use the <code>$_SERVER['DOCUMENT_ROOT']</code> variable</h2> <p>This is the absolute path to your document root: <code>/var/www/example.org/</code> or <code>C:\wamp\www\</code> on Windows.</p> <p>The document root is the folder where your root level files are: <a href="http://example.org/index.php" rel="noreferrer">http://example.org/index.php</a> would be in <code>$_SERVER['DOCUMENT_ROOT'] . '/index.php'</code></p> <p>Usage: <code>require_once $_SERVER['DOCUMENT_ROOT'] . '/include/otherfile.php';</code></p> <h2>Use an autoloader</h2> <p>This will probably be a bit overkill for your application if it's very simple.</p> <h2>Set up PHP's include paths</h2> <p>You can also set the directories where PHP will look for the files when you use <code>require()</code> or <code>include()</code>. Check out <code>set_include_path()</code> <a href="http://php.net/manual/en/function.set-include-path.php" rel="noreferrer">here</a>.</p> <p>Usage: <code>require_once 'otherfile.php';</code></p> <hr> <p><strong>Note:</strong></p> <p>I see some answers suggest using the URL inside a <code>require()</code>. I would not suggest this as the PHP file would be parsed before it's included. This is okay for HTML files or PHP scripts which output HTML, but if you only have a class definition there, you would get a blank result.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload