包含在php函数中

时间:2013-06-16 14:44:01

标签: php function templates include

我正在编写PHP代码,我有函数,在我已经包含的这个函数中,它运行正常但是如果我在这个包含文件中编写新代码我会收到错误

错误:

Notice: Undefined variable: text

功能:

function inc($templateinc){
    if(file_exists(Fsys.Fview.$templateinc)){
        include Fsys.Fview.$templateinc;
    }
    else {
        include Fsys.Fview.'errors/404.php';
    }
}

我在哪里打印功能:

$text = "text";
inc("main/index.php");

main / index.php文件:

echo $text;

如何解决此问题?

谢谢

3 个答案:

答案 0 :(得分:0)

而不是

inc("main/index.php");

include_once("main/index.php");

答案 1 :(得分:0)

   function inc($templateinc,$data){
    if(file_exists(Fsys.Fview.$templateinc)){
        include Fsys.Fview.$templateinc;
    }
    else {
        include Fsys.Fview.'errors/404.php';
    }
}

        $data=array();

    $data['title']='Some title...';
    $data['text']='This is page content...';

    $data['array']=array(1,2,3,4);



        inc('test.php',$data);

test.php的:

 echo $data['title'];

echo $data['text'];

foreach ($data['array'] as $var) {

    echo $var;

}

因此,$ data($ text)应作为参数传递给你的函数。

答案 2 :(得分:0)

不知道你想要达到的目标。 只需将$text = "text";放在main / index.php中 并将代码更改为inc("main/index.php"); echo $text;

基本上,$text未在index.php

中定义