The code snippet below generates '12290' from the second echo output but the third one is still zero.
How do I get a proper return value from mytest()
function?
$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>';
You forgot to assign the return value to the variable again.
It should be this:
$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>';
Instead of:
mytest(12290);
echo 'third: ' . $testId . '<br>';
Try:
echo 'third: ' . mytest(12290) . '<br>';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.