[英]How can I use a PHP function inside of another function?
我需要在另一个函数中使用一个函数,该怎么做? 我意识到该功能超出了范围,我还不了解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;
}
我不确定为什么这对我有用,但确实做到了:
<?php
function alpha ($test) {
$test = $test." ".$test;
return $test;
}
function beta ($var) {
echo alpha($var);
}
beta("Hello!");
//End Result : "Hello! Hello!"
?>
也许如果有人能够解释上述原因的话,将有助于回答总体问题?
函数read_random_string()返回密码字符串。 您可以例如将返回值分配给some_function()中的变量。
$password = readable_random_string($characters);
顺便说一句:从它的名字,我希望$字符包含....诸如'abc'或array('a','b','c')之类的字符,而不是长度。 尝试保持变量的名称“ speaking”。
也许您正在寻找此功能:
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"
原来没有问题,因为我一开始就以为是问题,因为它没有输出任何东西,问题是我的函数返回了,而不是打印到屏幕上,直到发布之前我才意识到这一点,对不起浪费的问题=(
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;
否则在调用函数时,我应该回显/打印出来
如果使用return $password
则必须使用$pass = readable_random_string($characters);
或者,如果您使用echo $password
则可以使用readable_random_string($characters);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.