w3resource

PHP Exercises : Get the document root directory as defined in the server's configuration file

PHP : Exercise-25 with Solution

Write a PHP script to get the document root directory under which the current script is executing, as defined in the server's configuration file.

Sample Solution:

PHP Code:

<?php
// getenv() gets the value of an environment variable named 'DOCUMENT_ROOT'
$rd = getenv('DOCUMENT_ROOT');

// Echo the value of the 'DOCUMENT_ROOT' environment variable
echo $rd . "\n";
?>

Explanation:

  • Get Environment Variable:
    • The getenv('DOCUMENT_ROOT') function retrieves the value of the environment variable named DOCUMENT_ROOT. This variable typically contains the root directory of the web server where the current script is running.
  • Store Value:
    • The retrieved value is stored in the variable $rd.
  • Output the Document Root:
    • The echo statement outputs the value of $rd, which represents the document root directory, followed by a newline (\n).

Output:

I:/Ampps/www

Flowchart:

Flowchart: Get the document root directory as defined in the server's configuration file

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to get the name of the owner of the current PHP script.
Next: Write a PHP script to get the information about the operating system PHP is running on.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-basic-exercise-25.php