0

I am wondering is there any way to get the value of a variable, that was defined in a loop inside a function?

Do variables declared in a loop / if-then-else, etc have a limited scope (only inside these blocks)?

here is an example of a function:

<?php

function getComments($tabName) {
    $sql = "select
                a.owner,
        a.table_name,
        a.column_name,
        a.data_type,
        a.data_length,
        a.data_precision,
        b.comments
            from 
                all_tab_columns a,
        user_col_comments b
            where 
                a.TABLE_NAME = b.table_name
        and a.COLUMN_NAME = b.column_name
        and a.owner = 'CORE' 
        and a.table_name ='" . $tabName . "'
            order by 
                a.column_id";
    $stid = oci_parse(getConnect(), $sql);

    // runs the query above                   
    oci_execute($stid);
    while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
        foreach ($row as $column => $entry) {
            // Column name
            if ($column == 'COLUMN_NAME') {
                $output = "this is a column";
            }
        }
    }

    return $output;
}

And here I get the "Undefined variable: output" error.

if I put echo instead the $output variable, I get the result.

Is it because the $output scope? How can I get the $output value in my return?

7
  • 1
    Seems that the $output variable isn't being set because the if-condition hasn't been satisfied. You should be starting with $output = ''; at the top of your function to avoid this issue. Commented Sep 16, 2014 at 14:31
  • thanks for the hint. However when I try $output = "" at the beginning, I just get an empty result in the output. It just remains unchanged Commented Sep 16, 2014 at 14:34
  • 1
    Along with @ʰᵈˑ's suggestion of using $output .= 'this is a column';, you should still be examining your if-statement, because it's clearly not being satisfied. You need to figure out why that's not happening. Commented Sep 16, 2014 at 14:38
  • Maybe change how you save the looped results, perhaps to an array. This way u can see what you are actually returning. Comment out the "if" line and make $output an array. That will send all variables to an array for evaluation Commented Sep 16, 2014 at 14:50
  • You're still ignoring the blatantly obvious problem of your if-statement from ever returning true... Or potentially, from the while or foreach loops never actually executing due to empty SQL results Commented Sep 16, 2014 at 15:00

1 Answer 1

1

try to satisfy this condition

        if ($column == 'COLUMN_NAME') {
            $output = "this is a column";
        }

and also u can try to return immediately after ur condition

        if ($column == 'COLUMN_NAME') {
            return "this is a column";
        }
Sign up to request clarification or add additional context in comments.

2 Comments

if I return immediately after condition, it shows an empty result. It seems it returns only outside the if/then/else and loop blocks
it was an error in my sql connection/statement logic, as I got "false" while fetching data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.