Jump to content

Function question


TapeGun007

Recommended Posts

After reading much on my searches in Google, I understand that you can call a function within a function.

 

But the one thing I can't seem to find is, can I call a function within a function?  Is there a sample code somewhere that I can look at and follow?

 

Example:

 

Function One();

{

}

 

Function Two();

{

One();

}

Link to comment
Share on other sites

After reading much on my searches in Google, I understand that you can call a function within a function.

 

But the one thing I can't seem to find is, can I call a function within a function?  Is there a sample code somewhere that I can look at and follow?

 

Example:

 

Function One();

{

}

 

Function Two();

{

One();

}

 

Maybe I don't fully understand the question, but you just called a function within a function with your example code. Function Two() called function One(), which is inside function Two().

 

I hope this helps,

 

- Stede

 

Link to comment
Share on other sites

You already did it.... here's a working simple example though.

 

<html>


<?php
function FUNC_ONE($test)
{
  $test = FUNC_TWO($test);
  return $test;
}

function FUNC_TWO($test_2)
{
$test_2 = $test_2 * 2;
return $test_2;
}

$bob = 10;
$bob = FUNC_ONE($bob);
Echo $bob;
// $bob will be 20
?>
</html>

Link to comment
Share on other sites

Ah ok, I see where I made a mistake in my little example.

 

I was referring to a 2nd function actually being inside another function is what I keep finding as an example... like this:

 

Function One();

{

  Function Two();

  {

  }

}

 

I mean literally "inside" another function.  The reason why I'm writing these functions is because I'm making the same calls over and over again within a framework of code.  Based upon certain conditions, there are slight variations.  Let me show you my incorrect code first.

 

// Define the array first outside the function to make it global.
$All_Files = array();
$Temp_Files = array();
$y=0;
// This function determines which files are to be displayed.
function Add_Files($file, $path, $y){
    global $All_Files;
    $y++;
    if(file_exists($path.$file)){
    	$File_Size=filesize($path.$file);
    }
    Else{
    	Echo "File doesn't exists, please contact the Administrator, $path, $file<p>";
    }
    $File_Size=$File_Size/1048576;
    $temp_size=number_format($File_Size,2);
    $Total_File_Size = $Total_File_Size + $File_Size;
    $All_Files[$y]=$file."|".$temp_size." MB's";
}

function ListFiles($path, $filetype)
{
    global $All_Files;
    global $y, $file, $path;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
/* Weed out the . and .. for going up one directory, so this keeps those from showing */
	if ( $file != '.' && $file != '..' ) {
	 // Eventually, I will put a Switch statement here for various conditions
	 Add_Files($file, $path, $y); 	
	}
}
}
closedir($handle);
natcasesort($All_Files);

 

Originally, I wrote this page with the same code over and over.  Then I just wanted to learn how to do functions, so here I am.  The code simply looks at all the files in a directory, stores the filename in a string, stores the filesize in another strings and combines the two.  Then it stores that one string into an array.  I do this because I can't sort a multi-dimensional array properly.

 

The code works fine without being all diced up into functions.  I'm just trying to stream line and learn more php.

 

The rest of the code just pulls up the array, splits the filename from the filesize, and displays the links.

 

$counter=1;
echo "<table border='0'>";
echo "<tr>";
echo "<td bgcolor='#333333' colspan='2' class='ContentWhite' align='center'> Controls </td><td bgcolor='#333333' class='ContentWhite' align='center' width='300'> File Name </td><td bgcolor='#333333' class='ContentWhite' align='center'> File Size </td>";
foreach ($All_Files as &$Value){
$counter++;
echo "<tr>";
// Get the position that the "|" exists in the string.
    $str_pos=strrpos($Value,"|");
$str_len=strlen($Value);
    $bgcolor = ($counter % 2)?"#ffffff":"#d9ece7";
?>

<td nowrap bgcolor="<? echo $bgcolor ?>"><a href="download_file.php?file=<? echo substr($Value,0,$str_pos) ?>">[ Download ]</a></td>
<td bgcolor="<? echo $bgcolor ?>"><a href="Music_Library/<? echo substr($Value,0,$str_pos) ?>">[ Open ]</a></td>
<td bgcolor="<? echo $bgcolor ?>"><? echo substr($Value,0,$str_pos)?></td>
<td nowrap align="right" bgcolor="<? echo $bgcolor ?>"><? echo substr($Value,$str_pos+1,strlen($Value)) ?></td>
<?php
}
echo "</table>";
include("components/footer.php");
?>

 

Originally, I wrote this as ONE function only, and I was able to pass the globals without issue.  Now I'm over my head trying to pass information from one function to another and could use some help.  $file, $path, and $y.

 

Thank you.

 

Link to comment
Share on other sites

I guess I'm at a lose here, I'm not sure if you can do that.... even if you could why would you need to?  A function is a function and can be called anytime... doesn't matter if it's outside the actual brackets of the first function... it executes at that moment you call it so as far as order of operations are concerned it runs the function call at that moment then executes the line after the function call. It's never needed to be physically within the first functions brackets.

 

But I think you are looking for a recursive function maybe?

Link to comment
Share on other sites

At the end of a function (the last line of code in the function) you can do a return, which will return a value to wherever it was called.

 

For example,

 

here's a function:

 

function temp()
{
return 1;
}

 

Now if you call that function:

 

$number = temp(); // number = 1

 

Get it? That's how you get a value out of a function.

 

Also, there's no rules when to call functions as long as they make sense. You can have a function call another function, which calls five more functions. You can have a function call itself, etc.

Link to comment
Share on other sites

Have you looked into directort iterator? http://us.php.net/manual/en/class.directoryiterator.php

 

Shows us all files and catalogues in directory except "." and "..".

<?php

foreach (new DirectoryIterator('../MYFOLDERDIRECTORYTOLOOKIN') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}

?>

 

Using this built in class may save you lots of headache now that I think of it.

Link to comment
Share on other sites

No, I didn't know anything about that.  I came from ASP (classic), and just trying to get up to speed on something newer (and cheaper).

 

I'll look into that!

 

thanks.

 

Yeah also if you have some questions in regard to try to get a hold of salathe.... http://www.phpfreaks.com/forums/index.php?action=profile;u=82906  He helped me when I first learned to use the directoryiterator and he wrote the manual on it... a very good resource.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.