What is chdir in PHP?

Overview

Chdir is a PHP function that changes the current directory to the directory specified in an argument.

Syntax


chdir(string $directory): bool

Parameters

The function takes in one parameter, dir.

dir is the path to the new current directory.

Return value

The function returns a boolean value. It returns true if the directory is changed successfully. Otherwise, it returns false.

Code

<?php
// prints the value of current directory
echo getcwd() . "\n";
// changes the current directory to "edpresso"
chdir("edpresso");
// prints the value of new current directory
echo getcwd();
?>

Output


/home/educative 
/home/educative/edpresso

Free Resources