I have the following code which works fine inline with my code:
if ($progressData[1] == "yes") {
echo "Complete";
}
else
echo "Not Yet Complete";
However, I would like to call it from a function:
function progressOutput () {
if ($progressData[1] == "yes") {
echo "Complete";
}
else
echo "Not Yet Complete";
}
When I call progressOutput(), I get “Not Yet Complete”, even though $progressData[1] is equal to “yes”.
Here is how I am calling the function:
Mission Status: <?php progressOutput(); ?>
What do I need to do to get progressOutput() to return “Complete” when $progressData[1] is in fact equal to “yes”?
You need to pass the variable from the calling scope into the function in order for the function to have access to it.
Your function should accept an argument:
And when you call it, you should provide an argument:
It’s typically a bad idea for your functions to output data directly; you could clean it up by returning a value instead of echoing it:
And outputting the value returned by the function: