I prefer to program in Python language, but have to work in PHP for a specific web site app project.
In PHP I am trying to "return" a value from a function to the main program environment (to be used for subsequent calculations), but no matter what I try the value calculated in my function is not returning the value (but echoing from function works fine).
In Python I never had an issue with returning variables: i.e. all values returned from functions were available and accessible to the main program and/or other functions that called the function that produces the return value.
Can someone please tell me how I can solve this issue? I have been searching google and sites ike stackoverflow for the last 2 days with no success. I have many O'Reilly computer books including many on PHP where I have cross referenced my research and read everything I can about the RETURN function - it seems I am doing everything right and even specifically declaring the value to be returned. It is critical that I am able to return values and have access to those values in order to proceed with development on this project - else I am stuck if I cannot return values to be processed further!!
Here is the relevant code pieces:
// DECLARE FUNCTIONS
// FUNCTION
function Calculation_IsArray ($ArrayAny){
if (is_array($ArrayAny)) {
echo '<div>THIS VAR IS AN ARRAY</div>';
$Var_IsArray = TRUE;
return $Var_IsArray;
} else {
echo '<div>THIS VAR IS **NOT** AN ARRAY</div>';
}
}
After declaring the functions and doing some initial calculations to grab an array for checking, I call the above function as follows:
// CALL FUNCTION
Calculation_IsArray($ArrayOfValues);
I am expecting the program to call the function Calculation_IsArray() and pass it the array to be checked ($ArrayOfValues). You can see from the output below that the array is passed and checked and confirmed to be of array type, and both the FOREACH loop as well as the IF/ELSE conditions are working fine and echoing the correct output. However, the above code does not return any values as you can see the NULL values that are echoed when I check for returned values (i.e. that array that was checked) accessible from the main program after the "return".
And here are the results echoed to browser screen (output):
THIS VAR IS AN ARRAY
RETURNED ARRAY =
NULL
VALUE OF $Var_IsArray =
NULL
COUNT OF ARRAY KEY ELEMENTS = 2
ARRAY KEY ELEMENTS ARE NUMERIC
KEY = 0, VALUE = Array
COUNT OF ARRAY KEY ELEMENTS = 2
ARRAY KEY ELEMENTS ARE NUMERIC
KEY = 1, VALUE = Array
I have searched here at stackoverflow and found reports of similar problems (and I even tried to test those suggestions for solutions, e.g. placing return at various places in my function to test where it would work), but nothing is working, and this failure to return value is not logical according to what I have read that PHP returns values if expliciting told to RETURN.
Thank you very much for any help in this matter!
[After submission of the original question above]:
I am now trying to isolate my problem by creating a test script called TestReturn.php.
In that script I have placed the following code, but still there is no value returned!!
// DECLARE FUNCTIONS
// FUNCTION
function Calculation_IsArray ($ArrayAny){
if (is_array($ArrayAny)) {
echo '<div>THIS VAR IS AN ARRAY</div>';
$Var_IsArray = TRUE;
return $Var_IsArray;
} else {
echo '<div>THIS VAR IS **NOT** AN ARRAY</div>';
}
}
// BEGIN MAIN PROGRAM
$x = array(1, 2, 3, 4, 5, 6, 7);
Calculation_IsArray($x);
// COME BACK TO HERE AFTER THE FUNCTION RETURNS CALCULATED VALUE
echo '<div>HERE IS THE RETURNED VALUE AND VAR_DUMP BELOW:' . $Var_IsArray . '</div>';
var_dump($Var_IsArray);
And here is the output in HTML to browser tab/window/terminal:
THIS VAR IS AN ARRAY
HERE IS THE RETURNED VALUE AND VAR_DUMP BELOW:
NULL
"Captain, this does not compute!", i.e. this doesn't make sense to me why PHP is not returning my value that I specifically tell it to return to main program!
I have tried all possibilities of coding including:
return $variable;
return ($variable);
return() ; // i.e. I read somewhere that PHP by default returns the last calculated value in a function.
return ;
Why is PHP not returning my value/variable back to main program?
$Var_IsArrayoutside it's function scope?