开发者

PHP: Use function outside current function

开发者 https://www.devze.com 2023-02-28 21:51 出处:网络
can I use a function that is outside the current function? eg. function one($test){ return 1; } function two($id){

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 i开发者_JS百科t then to use the function that are outside? Thanks

The function is in the same file.. /


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


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


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.


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


This should work fine Example:

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

echo run();
?>

Maybe you have an issue elsewhere?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号