亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確地從 PHP 函數中獲取返回值?

如何正確地從 PHP 函數中獲取返回值?

PHP
忽然笑 2022-06-17 16:07:07
下面的代碼片段從第二個回顯輸出生成“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>';


查看完整回答
反對 回復 2022-06-17
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

代替:

mytest(12290);
echo 'third: ' . $testId . '<br>';

嘗試:

echo 'third: ' . mytest(12290) . '<br>';


查看完整回答
反對 回復 2022-06-17
  • 2 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號