包含在php函数中

包含在php函数中,php,function,templates,include,Php,Function,Templates,Include,我正在编写PHP代码,其中有函数,在这个函数中有include,它可以正常工作,但如果我在这个include文件中编写新代码,就会出错 错误: Notice: Undefined variable: text 功能: function inc($templateinc){ if(file_exists(Fsys.Fview.$templateinc)){ include Fsys.Fview.$templateinc; } else {

我正在编写PHP代码,其中有函数,在这个函数中有include,它可以正常工作,但如果我在这个include文件中编写新代码,就会出错

错误:

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;
我如何解决这个问题

谢谢

而不是

inc("main/index.php");
试一试

test.php:

 echo $data['title'];

echo $data['text'];

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

    echo $var;

}

所以,$data($text)应该作为参数在函数中传递。

不知道您想要实现什么。 只需放入
$text=“text”内部main/index.php
并将代码更改为
inc(“main/index.php”);
echo$文本


基本上,
$text
不是在索引中定义的。php

我认为Giorgi试图创建某种框架/mvc模式,他想要将变量传递给视图的方法?我正在尝试创建模型和视图,在视图中我需要模板(html),在模型中我需要脚本。谢谢你的回复感谢你的重播,现在它的作品,但我有很多变量,当它很多,我不能添加所有的变量。感谢you@GiorgiBulia,制作数组,并在数组中放置变量。我不知道如何制作。现在我要添加全局$text;在这个函数中,我尝试这样做:$text->giorgi=“giorgi”$text->bulia=“bulia”;现在它打印出来了,但在标题中我得到了这个错误:严格标准:从空值创建默认对象。非常感谢。您将需要关联数组,甚至可能需要多维数组(数组的组合;取决于站点的复杂性)。“->”符号用于访问对象属性和方法。您没有创建对象,可能:@GiorgiBulia,请查看更新的答案。。。如果你想采用面向对象的方法,你需要类、对象等…我需要使用函数,谢谢你回答问题解决了!我加入全球$tpl;在函数中,我在模板中添加:$tpl[“giorgi”]=“giorgi”$tpl[“bulia”]=“bulia”;谢谢大家!!
   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);
 echo $data['title'];

echo $data['text'];

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

    echo $var;

}