Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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_Function_Scope - Fatal编程技术网

Php 函数中行为异常的变量数据

Php 函数中行为异常的变量数据,php,codeigniter,function,scope,Php,Codeigniter,Function,Scope,我正在开发CodeIgniter应用程序 我的应用程序的导航菜单的一部分是使用会话数据生成的。因为,我必须在许多地方打印相同的东西,所以我编写了一个函数来进行打印。创建菜单的文件如下所示。此文件中多次使用函数print\u roles\u assigned() $roles_assigned = $this->session->userdata('roles_assigned'); function print_roles_assigned() { $output = '';

我正在开发CodeIgniter应用程序

我的应用程序的导航菜单的一部分是使用会话数据生成的。因为,我必须在许多地方打印相同的东西,所以我编写了一个函数来进行打印。创建菜单的文件如下所示。此文件中多次使用函数
print\u roles\u assigned()

$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
    $output = '';
    if ($roles_assigned)
    {
        foreach ($roles_assigned as $role) {
            $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
        }
    }
    else
    {
        $output .= '<li>No roles have been assigned.</li>';
    }
    return $output;
}
我想知道:

  • 为什么我的初始代码无法工作
  • 使用
    $GLOBAL
    是否合适
  • 解决此问题的替代方法是什么

    • 这听起来像是一个范围问题。尝试使用
      $this->roles\u assigned
      引用该数组


      依我看,以这种方式使用GLOBAL是不好的做法。

      这听起来像是一个范围问题。尝试使用
      $this->roles\u assigned
      引用该数组


      依我看,以这种方式使用GLOBAL是不好的做法。

      初始代码失败,因为您在中分配的$roles\u没有被注入。将函数参数化为“function print_roles_assigned($roles_assigned){..}”,以访问$roles_assigned变量。

      初始代码失败,因为未注入分配的$roles_。将函数参数化为“function print_roles_assigned($roles_assigned){..}”,以访问$roles_assigned变量

      为什么我的初始代码无法工作

      每个函数都有所谓的变量作用域。在函数外部声明的变量不能从函数内部访问,除非它们作为参数传入、是函数所属类的成员或显式声明为全局变量

      有三种不同的方法可以做到这一点,随你选择

      最简单的方法是将函数作为参数传入,即

      $roles_assigned = $this->session->userdata('roles_assigned');
      function print_roles_assigned($roles_assigned) {
          $output = '';
          if ($roles_assigned)
          {
              foreach ($roles_assigned as $role) {
                  $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
      e_name) . '</li>';
              }
          }
          else
          {
              $output .= '<li>No roles have been assigned.</li>';
          }
          return $output;
      }
      
      另一个选项(不推荐)是使用全局关键字,即

      $roles_assigned = $this->session->userdata('roles_assigned');
      function print_roles_assigned() {
          global $roles_assigned;
          $output = '';
          if ($roles_assigned)
          {
              foreach ($roles_assigned as $role) {
                  $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
      e_name) . '</li>';
              }
          }
          else
          {
              $output .= '<li>No roles have been assigned.</li>';
          }
          return $output;
      }
      
      $roles\u assigned=$this->session->userdata('roles\u assigned');
      功能打印\u角色\u分配(){
      分配的全局$角色;
      $output='';
      如果($roles\u assigned)
      {
      foreach($roles\u分配为$role){
      $output.='
    • '.anchor('main/home/'.$role->role\u name,$role->rol e_name)。“
    • ”; } } 其他的 { $output.='
    • 未分配任何角色。
    • '; } 返回$output; }
      为什么我的初始代码无法工作

      每个函数都有所谓的变量作用域。在函数外部声明的变量不能从函数内部访问,除非它们作为参数传入、是函数所属类的成员或显式声明为全局变量

      有三种不同的方法可以做到这一点,随你选择

      最简单的方法是将函数作为参数传入,即

      $roles_assigned = $this->session->userdata('roles_assigned');
      function print_roles_assigned($roles_assigned) {
          $output = '';
          if ($roles_assigned)
          {
              foreach ($roles_assigned as $role) {
                  $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
      e_name) . '</li>';
              }
          }
          else
          {
              $output .= '<li>No roles have been assigned.</li>';
          }
          return $output;
      }
      
      另一个选项(不推荐)是使用全局关键字,即

      $roles_assigned = $this->session->userdata('roles_assigned');
      function print_roles_assigned() {
          global $roles_assigned;
          $output = '';
          if ($roles_assigned)
          {
              foreach ($roles_assigned as $role) {
                  $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
      e_name) . '</li>';
              }
          }
          else
          {
              $output .= '<li>No roles have been assigned.</li>';
          }
          return $output;
      }
      
      $roles\u assigned=$this->session->userdata('roles\u assigned');
      功能打印\u角色\u分配(){
      分配的全局$角色;
      $output='';
      如果($roles\u assigned)
      {
      foreach($roles\u分配为$role){
      $output.='
    • '.anchor('main/home/'.$role->role\u name,$role->rol e_name)。“
    • ”; } } 其他的 { $output.='
    • 未分配任何角色。
    • '; } 返回$output; }
      为什么不能使用
      $this->session->userdata('roles_assigned')
      函数内部?@user1612290它不起作用。为什么不能使用
      $this->session->userdata('roles_assigned')函数内部?@user1612290它不起作用。我的投票将是中间的例子,这里。这基本上是我的答案,放在一个众所周知的盘子里交给他。我的投票将是中间的例子,这里。这基本上就是我的答案,用一个众所周知的盘子递给他。