下面的代碼片段從第二個回顯輸出生成“12290”,但第三個仍然為零。如何從mytest()函數中獲得正確的返回值?$testId = 0;echo 'first: ' . $testId . '<br>';function mytest($postId) { if (get_post_type($postId) === 'artist') { $testId = $postId; } echo 'second: ' . $testId . '<br>'; return $testId;}mytest(12290);echo 'third: ' . $testId . '<br>';
2 回答

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
您忘記再次將返回值分配給變量。
應該是這樣的:
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
$testId = mytest(12290);
echo 'third: ' . $testId . '<br>';

搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
代替:
mytest(12290); echo 'third: ' . $testId . '<br>';
嘗試:
echo 'third: ' . mytest(12290) . '<br>';
- 2 回答
- 0 關注
- 165 瀏覽
添加回答
舉報
0/150
提交
取消