Jump to content

calling a function within a function


jimfog

Recommended Posts

I might as well post in the html forum... The problem I am facing is that calling a function within a function messes with the html. Here is the code where the html is OK:

function output_header($username=false) {?><div id="header">	 <div><a href="http://localhost/Appointments/Frontend/"><img src="Images/logo.png" width="307" height="32" /></a></div>	 <ul id="headelem">    <li><?php echo $username ?></li>	 <li><a href="calendar.php">Calendar</a></li>    <li><a href="login.php">log in</a></li>    <li><a href="register_form.php">register</a></li>    </ul></div><?php}

And here is the code where the html is messed, a function is called inside a function, so below you will see 2 functions:

function output_header_list($username){    ?> 	 <ul id="headelem">    <li><?php echo $username ?></li>		    <li><a href="calendar.php">Calendar</a></li>    <li><a href="login.php">log in</a></li>    <li><a href="register_form.php">register</a></li>    </ul> <?php   }function output_header($output_header_list=false) {?><div id="header">	  <div><a href="http://localhost/Appointments/Administrator/">			 <img src="Images/logo.png" width="307" height="32" /></a></div>	 <?php  $output_header_list ?>    </div><?php}

The problem is that instead of ul id="headelem" appearing within div id header(as depicted it the function output header) it appears out of it. Here is the image-from firebug:https://skydrive.live.com/redir?resid=BE27434B2AAC8130!243 Why the above happens?

Edited by jimfog
Link to comment
Share on other sites

You're not calling that other function correctly, but it sounds like it is outputting the HTML before the functions get called. Replace the output with a call to echo a heredoc and see if that makes a difference.

Link to comment
Share on other sites

You're not calling that other function correctly, but it sounds like it is outputting the HTML before the functions get called. Replace the output with a call to echo a heredoc and see if that makes a difference.
You're not calling that other function correctly, but it sounds like it is outputting the HTML before the functions get called. Replace the output with a call to echo a heredoc and see if that makes a difference.
Are you suggesting(if understand correctly) that I do not call a function in a function and just use echo statements? Edited by jimfog
Link to comment
Share on other sites

There's nothing inherently wrong with calling functions from other functions. In fact, calling functions is a basic part of functional or procedural programming. I'm suggesting that PHP might be outputting that HTML straight away instead of waiting until the function is called based on the fact that your screenshot shows those elements in the same order they appear in the code despite the fact that the second function that is defined gets called first. And also because the second function is definitely not calling the first function, I don't see that first function being called anywhere. This line: <?php $output_header_list ?> does not call a function. In fact, it doesn't do anything.

Link to comment
Share on other sites

There's nothing inherently wrong with calling functions from other functions. In fact, calling functions is a basic part of functional or procedural programming. I'm suggesting that PHP might be outputting that HTML straight away instead of waiting until the function is called based on the fact that your screenshot shows those elements in the same order they appear in the code despite the fact that the second function that is defined gets called first. And also because the second function is definitely not calling the first function, I don't see that first function being called anywhere. This line: <?php $output_header_list ?> does not call a function. In fact, it doesn't do anything.
Here is the call-not shown in the example depicted in the beginning of the post:
output_header(output_header_list($username));

With the above code, when output_header gets called, output_header_list($username)(which is the second function) gets passed as an argument. What do you think now?

Link to comment
Share on other sites

output_header_list() should have return something when yo you pass it to output_header(). output_header(output_header_list($username));it tells that use whatever output_header_list() is returning, as first parameter of output_header(). if it does not return anything it will be considered as null (value of first param of output_header()

Edited by birbal
Link to comment
Share on other sites

Right. You're not passing that function as an argument, you are running the function (which is why it displays that HTML code first), and then passing the return value of the function. It doesn't return anything, so you're passing a null value to the function. In PHP you pass the function name as a string and then use the variable as if it were a function:

output_header('output_header_list', $username); function output_header($func, $param){  ...  $func($param);  ...}

Link to comment
Share on other sites

output_header_list() should have return something when yo you pass it to output_header(). output_header(output_header_list($username));it tells that use whatever output_header_list() is returning, as first parameter of output_header(). if it does not return anything it will be considered as null (value of first param of output_header()
So you are saying that in order for a function to be passed as an argument it MUST return something.Ok how am I going to "return" that html ul list in output_header_list-how exactly am I going to use the return keyword-from a syntax point of view?
Right. You're not passing that function as an argument, you are running the function (which is why it displays that HTML code first), and then passing the return value of the function. It doesn't return anything, so you're passing a null value to the function. In PHP you pass the function name as a string and then use the variable as if it were a function:
output_header('output_header_list', $username); function output_header($func, $param){  ...  $func($param);  ...}

Your code works fine,nonetheless it is first time i see a function passed as an argument/string. Do we do this when the function we want to pass as a n argument to another function DOES not return something. There is a problem though... I call output_header 2 times in my script, one with arguments and one without. When it is with arguments(the code you gave ) it is ok.But with no arguments, I get the following error:
Fatal error: Function name must be a string in C:\Apache24\htdocs\Appointments\Administrator\output_functions_admin.php on line 443

when i wrote the function, I set the arguments to false so as not have any errors when it is called without them:

function output_header($output_header_list=false,$username=false) {?><div id="header">	  <div><a href="http://localhost/Appointments/Administrator/">			 <img src="Images/logo.png" width="307" height="32" /></a></div>	 <?php  $output_header_list($username) ?>    </div><?php}

What can we do?

Edited by jimfog
Link to comment
Share on other sites

So you are saying that in order for a function to be passed as an argument it MUST return something.Ok how am I going to "return" that html ul list in output_header_list-how exactly am I going to use the return keyword-from a syntax point of view?
http://www.php.net/manual/en/functions.returning-values.php
When it is with arguments(the code you gave ) it is ok.But with no arguments, I get the following error:
yo must have to match the declaring parameter numbers and calling parameter numbers, unless you are using any default value for any parameters. and if you want to use any variable function as callback. the value of variable must fullfill the criteria of valid function names.
Your code works fine,nonetheless it is first time i see a function passed as an argument/string. Do we do this when the function we want to pass as a n argument to another function DOES not return something.
it is work as callback. same callback we used in ajax or use usort() and other php functions. we use this when we want a changebale behaviour of a function at runtime or want to run a function multiple times inside a function. http://www.php.net/manual/en/functions.anonymous.phphttp://www.php.net/manual/en/functions.variable-functions.php
Link to comment
Share on other sites

yo must have to match the declaring parameter numbers and calling parameter numbers, unless you are using any default value for any parameters. and if you want to use any variable function as callback. the value of variable must fullfill the criteria of valid function names.
I really cannot find where is the error here,here are the functions:
function output_header($function=false,$username=false) {?><div id="header">	  <div><a href="http://localhost/Appointments/Administrator/">			 <img src="Images/logo.png" width="307" height="32" /></a></div>	 <?php  $function($username); ?>    </div><?php}function output_header_list($username){?>     <ul id="headelem">    <li><?php echo $username ?></li>	 <li><a href="calendar.php">Calendar</a></li>    <li><a href="login.php">log in</a></li>    <li><a href="register_form.php">register</a></li>    </ul> <?php   }

This works well:

output_header('output_header_list',$username);

This does not:

output_header() ;

I get the message I say above.As you see output_header gets declared with 2 arguments set at a default value of false-so there should not be a problem with calling the function withour arguments. According to the error message the problem lies at $function($username)-this is where the mistake is. But as I understand the above is needed(the way its syntax is) when calling output_header('output_header_list',$username);

Link to comment
Share on other sites

The way output_header() is currently written, the value of the first function is ALWAYS treated as the name of a function to be called, and the second argument is ALWAYS passed to that function.Your default values to both are false, but what this means is that PHP tries to call a function with a name that is a boolean false, which makes no sense, hence the error.You need to "manually" check if what you're given is the name of a function, or another arbitrary value (whether that's a boolean false, null, or a string that isn't a function name), and act accordingly. So for example, instead of

<?php  $function($username); ?> 

you can have

<?php if (is_callable($function)) {    $function($username);} ?>

Which would mean that if $function does not contain the name of a real function, nothing will happen.

Link to comment
Share on other sites

you declared output_header() and declare its first parameter default to false

function output_header($function=false,$username=false)
if you pass callback to first param like
output_header('output_header_list',$username);
callback output_header_list' is assigned to $function. and then you call it like variable function $function() when yo do likeoutput_header(); as you did not pass anything the default value will be set to $function which is false. after that when you try to call it using $function() which is not valid function name so thus the error. you can check using is_callable() that passed parameter is callable function or not.http://php.net/is_callable Edited by birbal
Link to comment
Share on other sites

well, what do you expect to happen when you set the default value of $func to false, and then try to call it like a function?

Link to comment
Share on other sites

well, what do you expect to happen when you set the default value of $func to false, and then try to call it like a function?
A small comment here.I set it to false because I assumed that $func is considered an argument of output_header. And arguments in PHP function CAN be set to false. And when you say "call it like a function"-you mean output_header() or output_header('output_header_list',$username) I assume you are referring to the first call. Edited by jimfog
Link to comment
Share on other sites

I set it to false because I assumed that $func is considered an argument of output_header. And arguments in PHP function CAN be set to false.
Yes, but within the function, the $function argument is called like a function. In other words, we're talking about the use of the argument, not the syntax of its declaration (which is fine). The phrase "call it like a function" refers to the line
$function($username);

THAT is calling a variable/argument like a function. If $function is set to 'output_header_list', then the above is equivalent to having

output_header_list($username);

in place of that line.

Link to comment
Share on other sites

I am getting now somewhere, nonetheless I am surprised that $function($username) is treated like a function,given the fact I put dollar sign in front of it, which means the purpose was to have it treated like a variable. Now we I must find the solution to the problem...callable etc.

Link to comment
Share on other sites

Your code works fine,nonetheless it is first time i see a function passed as an argument/string. Do we do this when the function we want to pass as a n argument to another function DOES not return something.
It doesn't matter if the function returns a value or not. This is how you call a variable function in PHP. http://www.php.net/manual/en/functions.variable-functions.php There are other ways to call functions: http://www.php.net/manual/en/function.call-user-func.phphttp://www.php.net/manual/en/function.call-user-func-array.php
Link to comment
Share on other sites

I am getting now somewhere, nonetheless I am surprised that $function($username) is treated like a function,given the fact I put dollar sign in front of it, which means the purpose was to have it treated like a variable.
Wrong, the parentheses tell it to treat it like a function, not a variable. The dollar sign tells it that the function name is stored in a variable.
Link to comment
Share on other sites

Me and birbal already gave you the solution. I even wrote down the code here.
And the solution works(many thanks)-nonetheless I have not understood yet the underlying logic of is_callable
Link to comment
Share on other sites

Let us see the code:

function output_header($function=false,$username=false) {?><div id="header">	  <div><a href="http://localhost/Appointments/Administrator/">			 <img src="Images/logo.png" width="307" height="32" /></a></div>    <?php if (is_callable($function)) {    $function($username);} ?>  </div><?php}

if the argument $function is a function then the output of that if statement would be $function($username);I do not understand how this stops the error from appearing. I don't get it...

Link to comment
Share on other sites

Because if $function is not callable, then the condition evaluates to false and it never tries to run the function.
Now I got it.There is one slight details though yet to complete the picture... We said $function is not callable-nonetheless, when output_header gets called, don;t we also call the inner function function(username) (by setting its value to false)which gave us the error and sparkled this all discussion? I hope you understand what I am talking about.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...