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?
$outputvariable 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.$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.$outputan array. That will send all variables to an array for evaluationwhileorforeachloops never actually executing due to empty SQL results