简体   繁体   English

如何在php中打印变量值?

[英]How to print variable value in php?

I have a variable named ' $uname ' in a function from controller and I need to print the ' $uname ' value in the ' login_form_success ' under view. 我在控制器的函数中有一个名为“ $uname ”的变量,我需要在视图下的“ login_form_success ”中打印“ $uname ”值。

login.php login.php

public function uname_exist($uname) {
        $this->db->where('username', $uname);
        $query = $this->db->get('login');
        if (!$query->num_rows() > 0) {
            $this->form_validation->set_message(__FUNCTION__, 'Invalid Username');
            return FALSE;
        } else {

            return TRUE;
        }
    }

login_form_success.php login_form_success.php

<div class="alert alert-success">Welcome <?php echo $uname; ?> Signed in !</div>

You can save the value in _SESSION array as 您可以将值保存在_SESSION数组中为

session_start();
$_SESSION['uname'] = $uname;

and use the same value anywhere by using $_SESSION['uname'] 并通过$_SESSION['uname']在任何地方使用相同的值

public function uname_exist($uname) {
        $this->db->where('username', $uname);
        $query = $this->db->get('login');
        if (!$query->num_rows() > 0) {
            $this->form_validation->set_message(__FUNCTION__, 'Invalid Username');
            $uname = "not exist";

        } else {
            $uname = "exist";

        }
        return $uname;
    }

use the $name return, and your html can got it by the same var name. 使用$ name返回,您的html可以使用相同的var名称获取它。

and better 更好

public function uname_exist($uname) {
        $this->db->where('username', $uname);
        $query = $this->db->get('login');
        if (!$query->num_rows() > 0) {
            $this->form_validation->set_message(__FUNCTION__, 'Invalid Username');
            $unamehave = 'new person';

        } else {
            $unamehave = $uname;

        }
        return $unamehave;
    }

<div class="alert alert-success">Welcome <?php echo $unamehave; ?> Signed in !</div>

This is only for future visitors: 这仅适用于将来的访客:

As you accepted the SESSION suggestion, i suggest you to use CI Session Library to follow framework standards: 当您接受SESSION建议时,我建议您使用CI会话库遵循框架标准:

You need to load session library in autoload.php as: 您需要按以下方式在autoload.php加载会话库:

$autoload['libraries'] = array('session');

now you can access the session library in any where in your project just because of autoload.php file, this will load session library automatically, now you can store session variables in your controller as: 现在,由于有autoload.php文件,您可以在项目中的任何位置访问会话库,这将自动加载会话库,现在您可以将会话变量存储在控制器中,如下所示:

$sessionArray = array('uname'=>$uname);
$this->session->set_userdata($sessionArray);

then, this session value should be accessible in your view file as like: 然后,应在您的视图文件中访问此会话值,如下所示:

echo $this->session->userdata('uname'); // will print your name

You can also load your session library inside your controller file, if you are not interested to load in autoload.php as: 如果您不希望以以下方式加载autoload.php ,也可以将会话库加载到控制器文件中:

$this->load->library('session'); // but this will not accessible in other controllers.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM