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"?

share|improve this question

77% accept rate
feedback

4 Answers

up vote 3 down vote accepted

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:

function progressOutput ($progressData) {
    if ($progressData[1] == "yes")
        echo "Complete";
    else
        echo "Not Yet Complete";
}   

And when you call it, you should provide an argument:

Mission Status: <?php progressOutput($progressData); ?>

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:

function progressOutput ($progressData) {
    if ($progressData[1] == "yes")
        return "Complete";
    return "Not Yet Complete";
}  

And outputting the value returned by the function:

Mission Status: <?= progressOutput($progressData); ?>
share|improve this answer
Thanks meagar! This did the trick! I had tried the first part, but forgot to also provide the argument when I called the function. Thanks again! – Mark Rummel Oct 27 '11 at 21:41
feedback

Look up variable scoping in PHP. PHP's variables do not "trickle" down to lower scopes and must be declared explicitly global in the lower scopes to become visible.

function progressOutput() {
   global $progressData;
   etc...
}

The other option is to pass in progressData as a parameter:

progressOutput($data);

function progressOutput($progressData) {
    ....
}
share|improve this answer
Thanks Marc B. Your second option worked. I haven't tried the first one, but I'm sure that would work as well. Thanks again! – Mark Rummel Oct 27 '11 at 21:42
feedback

The $progressData is not in the scope of your function You should pass it to your function.

function progressOutput ($progressData){ // it will be available here now }

share|improve this answer
feedback

Assuming you are using the function as shown above, this means that within the function this variable is not known.

In order to to that you could at global to the variable like this:

global $progressData;

after that you could use it.

Or you should pass the variable in the function:

progressOutput(progressData[1]);
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.