我有一個函數,並希望在完成時收集它的輸出日誌到電子郵件。它看起來是這樣的:從內部函數追加字符串到父變量
function blabla() {
$emailMessage = "some text";
$emailMessage .= "some more text";
function foo() {
for ($x = 1; $x <= 20; $x++) {
$emailMessage .= 'Call #' . $x;
// then doing an API call
return $response;
}
}
$res = foo();
$emailMessage .= "last line of text";
}
現在$emailMessage
看起來是這樣的:some text || some more text || last line of text
。來自內部函數的文本不被附加。
我怎樣才能得到它看起來像這樣:some text || some more text || call #1 || call #2 ||... || last line of text
------編輯------
感謝@kamal朋友我用一個全局變量。結果如下所示:
function blabla() {
global $emailMessage;
$emailMessage = "some text";
$emailMessage .= "some more text";
function foo() {
global $emailMessage;
for ($x = 1; $x <= 20; $x++) {
$emailMessage .= 'Call #' . $x;
// then doing an API call
return $response;
}
}
$res = foo();
$emailMessage .= "last line of text";
}
爲什麼要使用「內部函數」? – zoubida13