简体   繁体   中英

Use a returned value of a function from another file in PHP

I am using a function in a separate file, that gets should return me an array containing the enum values of a field:

<?php 
function getEnumValues($table, $field) {
$enum_array = array();
$query = "SHOW COLUMNS FROM '{$table}' WHERE Field = '{$field}'";
$result = mysqli_query($query);
$row = mysqli_fetch_row($result);
preg_match_all('/\'(.*?)\'/', $row[1], $enum_array);
return $enum_array;
}
?>

and in the main file, I do this:

<?php
require 'common.php';
for ($i=0; $i <= 5; $i++) {
                getEnumValues(property, value_type);
                echo "<input type='radio' name='tipo_valor' value='$enum_array[$i]' required>" . '  ' .$enum_array[$i] . '<br>';
            }
?>

The problem is that the function returns nothing.

So is the function ok for what I need? And can I use a variable returned in another file, as a local variable?

Thanks for your time, and suggestions!

The function is returning a value; you're just not capturing it. The $enum_array in your function body only lives in the scope of that function. You need to put the return value of the function in a variable in scope. In your main file:

<?php
require 'common.php';
for ($i=0; $i <= 5; $i++) {
            $enum_array = getEnumValues(property, value_type);
            echo "<input type='radio' name='tipo_valor' value='$enum_array[$i]' required>" . '  ' .$enum_array[$i] . '<br>';
        }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM