簡體   English   中英

PHP變量作用域內的循環功能

[英]php variable scope inside a loop in a function

我想知道是否有任何方法可以獲取在函數內部的循環中定義的變量的值?

在循環/ if-then-else等中聲明的變量的作用域是否有限(僅在這些塊內)?

這是一個函數示例:

<?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;
}

在這里,我得到“未定義的變量:輸出”錯誤。

如果我將echo代替$output變量,則會得到結果。

是因為$output范圍? 如何獲得退貨中的$output值?

嘗試滿足這個條件

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

而且您也可以嘗試在您的情況下立即返回

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM