Jump to content

Why does this function work?


Chappers

Recommended Posts

Hi, I found the following function which I've altered slightly for my purposes, but in function remains as I found it. I'm trying (and struggling a bit) to fully understand writing functions and can't see why this works:

 

<?php
$dirname = "files/folder_to_delete";

function delete_directory($dirname) { 
if (!is_dir($dirname)) {
echo "Folder $dirname is not a directory or does not exist.";
}
else {
$dir_handle = opendir($dirname);
if (!$dir_handle) {
echo "Could not open directory.";
}
else {
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file)) {
unlink($dirname."/".$file);
}
else {
delete_directory($dirname.'/'.$file);
}}}
closedir($dir_handle);
rmdir($dirname);
echo "Directory and its contents successfully deleted.";
}}}

delete_directory($dirname);
?>

 

I've executed the function with delete_directory($dirname); at the end, because I've assumed that it must come after the function code?

 

Anyway, what I don't understand is that the function is delete_directory($dirname), so how can part of the function's code state else { delete_directory($dirname.'/'.$file); } which is effectively running the newly discovered directory through the function again using the variable $dirname.'/'.$file, when the function is already set to work on the variable $dirname, since the function is function delete_directory($dirname)? Or am I making a mistake by thinking that $dirname.'/'.$file is a new variable name...is it actually keeping the variable's name of simply $dirname, but adding the $file variable data to the end of the pre-existing $dirname data? I hope I've explained that in a way which is even slightly understandable...

 

Thanks for your time,

James

Link to comment
https://forums.phpfreaks.com/topic/106113-why-does-this-function-work/
Share on other sites

<?php 
function delete_directory($dirname) { 
//Checks to see if the directory is a directory
if (!is_dir($dirname)) {
echo "Folder $dirname is not a directory or does not exist.";
}
else {
//Checks to try to open the directory
$dir_handle = opendir($dirname);
if (!$dir_handle) {
//If it cannot open the directory Then do the following
echo "Could not open directory.";
}
else {
while($file = readdir($dir_handle)) {
//Checks to see if the file is equal to . or ..
if ($file != "." && $file != "..") {
//If the directory is not equal to the following directory/file 
if (!is_dir($dirname."/".$file)) {
//Then recmove the file and the directory
unlink($dirname."/".$file);
}
else {
//runs the function for removing the files
delete_directory($dirname.'/'.$file);
} } }
//This closes the directory
closedir($dir_handle);
//This removes the directory
rmdir($dirname);
//This displays that the Directory and its contents have been removed
echo "Directory and its contents successfully deleted.";
} } }

delete_directory($dirname);

?>

The "function", before the actual function means it's setting a function. the $dirname variable relates to the function

 

for example

 

function myFunction($var1, $var2)
{
echo $var1 . $var2;
}

 

The above code means, that when you call the function by typing

 

$var1 = 'hello ';
$var2 = 'world!';

myFunction($var1, $var2)

 

It takes the data from the variables $var1 and $var2 are, and uses it in the function. So if you take the above function, but instead of $var1 and $var2, you use 'hello ' and 'world!' like this

 

myFunction('hello ', 'world!')

 

The function takes the values and uses them, so it would echo 'hello world!'

 

So if you do

 

$hi = 'hi. ';
$hello = 'hello.';

myFunction($hi, $hello)

 

The function is actually

 

myFunction($hi, $hello)
{
echo $hi . $hello;
}

 

So you set the function first

 

function NameOfFunction($varSomething)
{
echo $varSomething;
}

 

Then you set the variables

 

$hi = 'hi';

 

Then you call the function, replacing $varSomething with the variable you want the function to echo

 

NameOfFunction($hi)

 

This will echo the word 'hi' because you used $hi, so the function is

 

NameOfFunction($hi)
{
// Prints the variable used when calling the function
echo $hi;
}

 

Hope that helps explain it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.