Content · Programming · PHP

PHP getcwd() Function: Usage and Examples

Understand PHP getcwd() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use getcwd() when you need to returns the absolute path of the current working directory in a PHP application. The examples below show the actual operation and its limitations.

What Is getcwd() in PHP?

Returns the absolute path of the current working directory. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

getcwd(): string|false

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

echo getcwd() !== false ? "Working directory available" : "Unavailable";

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

var_dump(is_string(getcwd()));

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$workingDirectory = getcwd(); if ($workingDirectory !== false) echo basename($workingDirectory);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

The current directory can change during execution and is not necessarily the script directory.

Comparison With Related Functions

ApproachDifference
getcwd()Returns the absolute path of the current working directory.
Related operation__DIR__ identifies the source file directory; getcwd() reads the process working directory.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationThe current directory can change during execution and is not necessarily the script directory.
Confusing this function with a related operation__DIR__ identifies the source file directory; getcwd() reads the process working directory.

Key Takeaways

  • Returns the absolute path of the current working directory.
  • The current directory can change during execution and is not necessarily the script directory.
  • __DIR__ identifies the source file directory; getcwd() reads the process working directory.

Use getcwd() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.