1

can I use a function that is outside the current function?

eg.

function one($test){
return 1;
}
function two($id){
one($id);
}

Seems like i cant, how should I do it then to use the function that are outside? Thanks

The function is in the same file.. /

1
  • 2
    If one couldn't do that, then functions could only call themselves which is a pretty limited use of functions. So, yes, of course you can do it like that and the code above is perfectly valid. Why do you think it does not work? Just calling two(5) would not produce any output, so you cannot know whether it "worked" or not. In any case, have a look at: php.net/manual/en/language.functions.php Commented Apr 24, 2011 at 8:23

5 Answers 5

5

Is your function inside of a class? In that case you have to use $this->function() instead of function().

Sign up to request clarification or add additional context in comments.

Comments

4

That's perfectly valid. Check out the running code here.

Comments

3

Your code looks valid to me : you are declaring two functions, called one and two ; and two is calling one.

Then, you can call any of those functions, to execute it.


For example, if you execute the following portion of code :

function one($test){
    var_dump(__FUNCTION__);
    return 1;
}
function two($id){
    var_dump(__FUNCTION__);
    one($id);
}

two('plop');

Note that I called two, in the last line of this example.


You'll get this kind of output :

string 'two' (length=3)

string 'one' (length=3)

Which shows that both functions were executed.

Comments

1

That works fine. However, one ignores its parameter. Then, two ignores the return value from one.

Comments

0

This should work fine Example:

<?php
function test ($asd)
{
    return $asd;
}
function run ()
{
    return test('dd');
}

echo run();
?>

Maybe you have an issue elsewhere?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.