2013-06-29 75 views
-1

我有a.php文件php从另一个文件中调用另一个函数的变量并将其传递给一个数组

它有4个函数。每个函数具有可变

功能1已$ VAR1 函数2已经$ VAR2 功能3已经$ VAR3 function4已经$ VAR4

我给自己定的所有四个变量作为全球

我有另一个文件b.php

我想从a.php只会调用这些4个变量和b.php把它们放在阵列

我所做的是这个(但它没有工作):

我添加

include ('a.php file path'); 
$myArray = array($var1,$var2,$var3,$var4); 
+0

显示的内容'a.php' – Winston

+4

你应该张贴您的代码,而不是去解释它。 – jeroen

+0

确保你在函数中设置变量global也 –

回答

0

您需要使用global keyword。以下是关于变量范围的内容:

  • 在类或函数中声明的变量会自动作用于该类或函数。
  • 在任何类或函数之外声明的变量默认为全局变量。
  • 如果你想在一个函数中使用全局变量,你必须使用全局关键字,这样PHP知道你的意思是使用全局的,而不是一些名字相同的本地。

下面是一个例子,仅使用$ var1。其他变量将使用相同的技术。

b.php

$var1 = "blah"; 

a.php只会

// including b.php causes the global $var1 to be defined 
include "b.php"; 

// outside of any class or function, $var1 refers to the global 
echo ($var1 . "\n"); // prints "blah" 

function x() { 
    $var1; // This is a local variable, not the global one. 

    // within this function, $var1 has not been defined, so you get a blank 
    // (or perhaps a Notice depending on your log settings) when you try 
    // to print it out. 
    echo ($var1."\n"); 
} 

function y() { 
    // This is how you tell PHP you wish to use a global variable from 
    // inside a function. 
    global $var1; 

    echo ($var1."\n"); 
} 

x(); 
y(); 
+0

这就是我做的,它没有工作 我试图解决这个问题,所以开始从a.php中删除代码来找出问题出在哪里。 我结束了与a.php文件与唯一,并且我的意思只是,此代码: <?php global $ x = 1; ?> 没有功能,没有其他的东西。它不起作用! 现在,b.php文件,实际上是图形验证码: http://jpgraph.net/features/src/show-example.php?target=new_line1.php 我试图拔出从a.php数组值作为变量 我想知道现在如果该代码可能不接受“包括”出于某种原因? –

+0

您正在使用全局关键字错误。请参阅我的答案更新。 – slashingweapon

0

我已经解决了它这样做:

我合并a.php只会与b.php,所以将b。 PHP文件不需要额外的“包含”命令,我认为它从来没有与jpgrah一起工作

之后,我只是调用变量来回像这样的功能里面L:

function1($var1); 
function2($var2); 
function3($var3); 
function4($var4); 

然后设置array($var1,$var2,$var3,$var4);

这里面工作的价值!

谢谢

相关问题