2009-09-07 103 views
-1

我需要在另一個函數裏面使用一個函數,我該怎麼做?我意識到函數超出了範圍,我不明白OOP和類。有人能幫我嗎?這裏如何在另一個函數中使用PHP函數?

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是其他功能的要求

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

您可以顯示readable_random_string函數也是如此,所以我們可以看到範圍問題 – bumperbox 2009-09-07 03:38:36

+0

絕對可以做到,它是大多數編程語言的基礎之一。你遇到的問題究竟是什麼? – Aziz 2009-09-07 03:38:58

+0

@ bumperbox我添加了那部分 – JasonDavis 2009-09-07 03:44:31

回答

3

我不知道爲什麼這個工作對我來說,但它做到了:

<?php 

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

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

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

也許如果有人能解釋爲什麼上述工作,這將有助於回答整體問題?

+0

閱讀我的文章上面,這是我的一個錯誤 – JasonDavis 2009-09-07 03:56:07

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" 
1

函數readable_random_string()返回密碼字符串。你可以例如將該返回值分配給some_function()中的變量。

$password = readable_random_string($characters); 

順便說一句:從它的名字我期望$字符包含....像 'ABC' 或陣列( 'A', 'B', 'C')的字符,而不是長度。儘量保持變量名稱「發言」。

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

回報$密碼;

應該

echo $password; 

否則調用函數時,我應該回音/打印出來

+0

隨着你的srand,密碼只有1000000個可能性。 (((double)microtime()* 2000000000)+(time()%2000)+1); – 2009-09-07 04:17:44

-1

如果你使用return $password那麼你必須使用$pass = readable_random_string($characters);

,或者如果你使用echo $password,那麼你可以使用readable_random_string($characters);

相關問題