简体   繁体   中英

How can I use a PHP function inside of another function?

I need to use a function inside of another function, how can I do this? I realize that the function is out of the scope, I do not understand OOP and classes yet. can someone help me?

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);
}

UPDATE here is the other function as requested

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;
}

I'm not sure why this worked for me, but it did:

<?php

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

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

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

Perhaps if someone could explain why the above works, it would help to answer the overall question?

function readable_random_string() returns the password string. You can eg assign that return value to a variable in some_function().

$password = readable_random_string($characters);

btw: From its name I would expect $characters to contain ....characters like 'abc' or array('a', 'b', 'c'), not the length. Try to keep the names of variables "speaking".

Perhaps you are looking for this feature:

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"

There turns out there is no scope problem as I thought there was at first because it was not outputting anything, the problem was my function has a return instead of printing to screen and I didn't realize it until this was posted, so sorry for the wasted question =(

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;
}

return $password;

should be

echo $password;

Or else when calling the function I should echo/print it out

if you use return $password then you have to use $pass = readable_random_string($characters);

or if you use echo $password then you may use readable_random_string($characters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM