Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function as the argument of another function?

Status
Not open for further replies.

fowlerlfc

MIS
Mar 20, 2002
136
US
I'm using php 4.2.1. Can a function be an argument for another function?

Here's a little test script I've tried:
<?
//test functions
function neato ($content) {
echo &quot;<html><head><title></title></head><body><table><tr><td>$content</td></tr></table></body></html>&quot;;
}

function content() {
echo &quot;<table border=\&quot;1\&quot;><tr><td>this is a test</td></tr></table>&quot;;
}

//try it by assigning content function to a variable
$test = content();
neato($test);
//or by making it the argument directly
neato(content());
?>

When I test this I always get the output of content() before the output of neato(). When I want the output of content() to be inside the output of neato(). Does this make sense? Is it possible?

Thanks in advance for your help.
 
i guess i should rtfm in it's entirety! thanks so much!
 
Good luck reading PHP's manual in it's entirety.

I think printed out, it's about 1500 pages. ______________________________________________________________________
TANSTAAFL!
 
What you try to apply is officially called a 'callback' function.
Do it like this in PHP...

function writeTD($td)
{
echo &quot;<td>&quot;.$td.&quot;</td>\n&quot;;
}

function writeLI($li)
{
echo &quot;<li>&quot;.$li.&quot;\n&quot;;
}

function outputArray(&$someArray, $cbFnc)
{
$len = sizeof($someArray);
$i=0;
while($i < $len){
$cbFnc($someArray[$i]); // Here it is...
$i++;
}
}

/* In you code */

...<table>
...<tr>
/* just pass the name of the function as a string to $cbFnc*/
outputArray($myArray, &quot;writeTD&quot;);
...</tr>
...</table>

OR
...<ul>
//single quotes work fine also
outputArray($myArray, 'writeLI');
...</ul>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top