如何在另一个函数中使用PHP函数?

时间:2009-09-07 03:37:03

标签: php

我需要在另一个函数内部使用函数,我该怎么做?我意识到该函数超出了范围,我还不了解OOP和类。有人能帮我吗?

function some_function ($dfd, $characters, $dssdf, $sdfds){

    $sdfs = 'sdfsdf'; //random stuff goes on

    //this is where my trouble begins I have a scope issue, I need to call this function inside of this function
    readable_random_string($characters);
}

此处更新是请求的其他功能

function readable_random_string($length = 6) {
    $conso = array("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r",
        "s", "t", "v", "w", "x", "y", "z");
    $vocal = array("a", "e", "i", "o", "u");
    $password = "";
    srand((double)microtime() * 1000000);
    $max = $length / 2;
    for ($i = 1; $i <= $max; $i++) {
        $password .= $conso[rand(0, 19)];
        $password .= $vocal[rand(0, 4)];
    }
    return $password;
}

5 个答案:

答案 0 :(得分:3)

我不确定为什么这对我有用,但确实如此:

<?php

function alpha ($test) {
        $test = $test." ".$test;
        return $test;
}

function beta ($var) {
        echo alpha($var);
}

beta("Hello!");
//End Result : "Hello! Hello!"
?>

也许如果有人能够解释为什么上述有效,那么回答整体问题会有所帮助吗?

答案 1 :(得分:1)

function readable_random_string()返回密码字符串。你可以,例如将该返回值分配给some_function()中的变量。

$password = readable_random_string($characters);

btw:从它的名字我希望$字符包含......字符,如'abc'或数组('a','b','c'),而不是长度。尽量保持变量名称“说”。

答案 2 :(得分:0)

也许您正在寻找此功能:

function foo($message) {
    $function = 'bar';
    $function($message); // calls bar(), or whatever is named by $function
}

function bar($message) {
    echo "MESSAGE: $message\n";
}

foo("Hello"); // prints "MESSAGE: Hello"

答案 3 :(得分:0)

事实证明没有范围问题,因为我认为最初是因为它没有输出任何东西,问题是我的功能有一个返回而不是打印到屏幕,我没有意识到它发布之前,很抱歉浪费的问题=(

function readable_random_string($length = 6) {
    $conso = array("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r",
        "s", "t", "v", "w", "x", "y", "z");
    $vocal = array("a", "e", "i", "o", "u");
    $password = "";
    srand((double)microtime() * 1000000);
    $max = $length / 2;
    for ($i = 1; $i <= $max; $i++) {
        $password .= $conso[rand(0, 19)];
        $password .= $vocal[rand(0, 4)];
    }
    return $password;
}

返回$ password;

应该是

echo $password;

或者在调用函数时我应该回显/打印出来

答案 4 :(得分:-1)

如果您使用return $password,则必须使用$pass = readable_random_string($characters);

或者如果您使用echo $password,则可以使用readable_random_string($characters);