Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:57:11+00:00 2026-06-10T17:57:11+00:00

I have a viewer helper function that loads the main content alongside the footer/header.

  • 0

I have a viewer helper function that loads the main content alongside the footer/header. The bug/unexpected behavior occurred when I loaded the array’s key for the header that shares the same name for a variable in the main content view – the same array is loaded for both the header and main content.

I thought it’s normal, since the same $data array was sent to the header and main content as-well(as mentioned before). So the variable will naturally be present in both views. But, well, it wasn’t exactly like that. I unset the $data variable after sending the data to the header then re-created it when I wanted to send some data to the main view – but still the problem is not fixed.

I made a simple example for this bug/unexpected behavior:

Consider this view, named test:

<?php 
echo $some_data;  

And this controller:

class Test extends CI_Controller {

    function index() {

    $data['some_data'] = 'Some data.';
    $this->load->view('test', $data);
    /* 
     * Output:
     * Some data.
     */
    unset($data);
    unset($data['some_data']);//Just to make sure it's not PHP's fault.
    $this->load->view('test');
    /*
     * Output:
     * Some data.
     * 
     * Even though the $data variable is unsetted AND not passed!
     */

    $different_data = array();

    $this->load->view('test', $different_data);     
    /*
     * Output:
     * Some Data.
     * 
     * Still outputs the same thing, even though
     * I'm sending different array(and the $data array is unstted).
     * 
     */ 

    }
}  

Note: The whole code will output Some data. three times.

The only way to solve this issue is sending a different array and setting the array key(which is some_data) to something else which will override the old one.

So, is this a bug or something made by CodeIgniter’s dudes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:57:13+00:00Added an answer on June 10, 2026 at 5:57 pm

    This is expected behavior.

    Once variables are set they become available within the controller class and its view files. Sending an array in $this->load->view() is the same as sending an array directly to $this->load->vars() before calling the view file. This simplifies things for most people using multiple views in a controller. If you are using multiple view files in a single controller and want them to each have their own set of variables exclusively, you’ll need to manually clear out the $this->load->_ci_cached_vars array between view calls.

    A code comment in the Loader class describes another situation showing why this is the desired default behavior:

    //You can either set variables using the dedicated $this->load_vars()
    //function or via the second parameter of this function. We'll merge
    //the two types and cache them so that views that are embedded within
    //other views can have access to these variables.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page viewer as main window showing an external page as we
I have a scroll viewer in my application that contains a canvas, in which
I have used DLL Export viewer to try and find the functions that are
I have noticed that when you view PDFs in google docs the PDF viewer
I have this panorama viewer that makes use of java, but when trying to
So I have a photo viewer that I am creating to learn Silverlight and
Do we have any Debug viewer free tool for Java that we use for
I have a Page Viewer webpart that displays /_layouts/google.htm and needs changing. I applied
I have a WPF database viewer application: It's a simple main window containing a
We have a 3D viewer that uses OpenGL, but our clients sometimes complain about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.