从包含fil的函数中获取变量

时间:2018-02-01 21:46:59

标签: php web

我尝试使用外部文件中<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath=".\MyDotNetCoreApp.exe" /> </system.webServer> </configuration> 的变量。为实现此目的,我使用include function将该文件(functions.php)包含到我的主文件(insert.php)中并尝试function来自echo变量

我的主要文件是 insert.php ,其中包括以下内容:

functions.php

functions.php 档案:

<html>
    <head></head>
    <body>
        <?php
           include 'functions.php';
           external_functin();
           echo $primary_var0."<br />";
           echo $secondary_var0;
        ?>
    </body>
</html>

问题在于,每次我加载/运行insert.php时,由于它具有<?php function external_function(){ include 'mysql_connection.php'; $query="SELECT * FROM db_table"; $exec=mysqli_query($conn, $query); $i=0; if(mysqli_num_rows($exec)>0){ while($result=mysqli_fetch_assoc($exec)){#This while script stores the query result values to variables. ${"primary_var".$i} = $result['field_name']; ${"secondary_var".$i} = $result['id']; $i++; } } } ?> echo$primary_var0的语法,我得到$secondary_var0

非常感谢帮助。

3 个答案:

答案 0 :(得分:2)

在函数范围之外,函数external_function()内创建的变量将不可用。

您至少有两种方法可以获得它们:

a)参考

// functions.php
function external_function(&$primary, &$secondary) {
   //...
   $primary = $primary_var0;
   $secondary = $secondary_var0;
}

// insert.php
$primary_var0;
$secondary_var0;
external_function($primary_var0, $secondary_var0);
echo $primary_var0;
echo $secondary_var0;

b)返回值

// functions.php
function external_function() {
   //...
   return [$primary_var0, $secondary_var0];
}

// insert.php
list($primary_var0, $secondary_var0) = external_function();
echo $primary_var0;
echo $secondary_var0;

c)使用array使用变量名称和值+ extract函数

// functions.php
function external_function(){
    // its just simulation of results from Database
    $resultsFromDB = [];
    $resultsFromDB[] = ['id' => 1, 'field_name' => 'something'];
    $resultsFromDB[] = ['id' => 2, 'field_name' => 'other'];
    $resultsFromDB[] = ['id' => 3, 'field_name' => 'value'];

    $i = 0;
    $variables = [];
    foreach($resultsFromDB as $result) {
        $variables["primary_var".$i] = $result['field_name'];
        $variables["secondary_var".$i] = $result['id'];
        $i++;
    }   

    return $variables;
}

// insert.php
$variables = external_function();
extract($variables);

echo $primary_var0 . ' ' . $primary_var1 . ' ' . $primary_var2 . '<br>';
// something other value

echo $secondary_var0 . ' ' . $secondary_var1 . ' ' . $secondary_var2;
// 1 2 3

答案 1 :(得分:1)

您应该返回功能external_function的数据。    所以你可以试试这个

 <?php          
    function external_function(){
                include 'mysql_connection.php';
                $query="SELECT * FROM db_table";
                $exec=mysqli_query($conn, $query);
                $i=0;
                if(mysqli_num_rows($exec)>0){
                    while($result=mysqli_fetch_assoc($exec)){#This while script stores the query result values to variables.
                    $data['primary_var'][$i] = $result['field_name'];
                    $data['secondary_var'][$i] = $result['id'];
                    $i++;
                    }
                  return $data ;
                }

        }
?>

您可以在此视图中访问您的数据

<html>
<head>

</head>
<body>
    <?php
    include 'functions.php';
    external_functin();
    echo $data['primary_var'][0]."<br />";
    echo $data['secondary_var'][0];
    ?>
</body>
</html>

答案 2 :(得分:0)

您需要在functions.php中定义external_function()并返回一些值,如下所示:

function external_function(){
    return 'some value';
}

然后当您在insert.php中调用external_function()时,如此:

echo external_function();

它会回应:&#34;某些价值&#34;

如果要返回多个值,可以返回如下数组:

return array('key1'=>'val1', 'key2'=>'val2');