I wonder why I am getting the result as 4
<?php
function minusNum($aNum){
$result=$aNum-1;
return $result;
}
$theNum=4;
minusNum($theNum);
echo $theNum;
?>
Your minusNum
function returns a new value, it doesn't manipulate the inputted $aNum
. You should use that returned value. Eg:
$result = minusNum($theNum);
echo $result;
You need to capture the result from your function.
<?php
function minusNum($aNum){
$result = $aNum - 1;
return $result;
}
$theNum = 4;
$theNum = minusNum($theNum);
echo $theNum;
?>
Here you assign the result of the function back to your original variable $theNum
.
Although you pass the variable into the function, it is only altered inside the function. The value remains the same outside of the function.
<?php
function minusNum($aNum){
$result=$aNum-1;
return $result;
}
$theNum=4;
echo minusNum($theNum);
?>
Echo the function because it returns result but when you echo $theNum you will get the $theNum value which is 4. Moreover the function will grape the value of any value variable that passes through not the variable it self. so you contradicting youself. I hope it helps.
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.