Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将变量从控制器传递到视图_Php_Codeigniter 2 - Fatal编程技术网

Php 如何将变量从控制器传递到视图

Php 如何将变量从控制器传递到视图,php,codeigniter-2,Php,Codeigniter 2,iam使用codeigniter 2.2 下面是我的控制器类 class Welcome extends CI_Controller { public function index() { $test = "hello"; $this->load->view('welcome_message',$test); } } 以下是我的看法 <html><body><h1><?php echo $data?>

iam使用codeigniter 2.2 下面是我的控制器类

class Welcome extends CI_Controller {


public function index()
    {
     $test = "hello";
     $this->load->view('welcome_message',$test);
 }
}
以下是我的看法

<html><body><h1><?php echo  $data?></h1></body></html>

试图将变量传递给视图时,会出现此错误

遇到一个PHP错误

严重性:通知

消息:未定义变量:数据

文件名:views/welcome_message.php

电话号码:75

试试这个

class Welcome extends CI_Controller {


    public function index()
        {
            $test = "hello";
            $data['test'] = $test
            $this->load->view('welcome_message',$data);
        }
    }
}
鉴于


有关更多参考信息,请检查哪里是
$data
set?R.T.?。我们能否将简单的字符串变量传递给视图??我尝试了以下代码,但仍然出现上述错误。它一直说遇到了PHP错误严重性:注意消息:未定义变量:数据文件名:views/welcome_Message.PHP行号:75@KevalRathi是的,我已经尝试了上面的代码,但仍然给我相同的错误。事实上,我不能发送任何变量到视图,即使是string@muhammadarslanTariq在您看来,@RohitGaikwad的代码更改是的,成功了,非常感谢。。请告诉我们不能传递任何字符串变量,如$myvar=“hello”;到视图并在那里打印?在控制器中表示$this->load->view('welcome_message',$data);鉴于echo myvar;您可以像这样使用$data['test']=“一些字符串”