A CodeIgniter view does not run in the global scope

In a regular PHP file, you can grab the value of a variable from the global scope using the keyword GLOBAL. So for example if you want to use a variable inside your function, you can do something like this…

$TODAY = date('Y-m-d');
function getEvents()
    if ($TODAY > '2012-02-14')

I’m not saying you SHOULD write code this way, but there are times when this sort of option is cleaner than passing another parameter to your function, or faster than rewriting the existing code.

I tried to do this in a CodeIgniter view, which looks and behaves very much like a regular PHP file. But it wasn’t working. My variables (such as $TODAY, above) weren’t available inside my function, even after I used the GLOBAL keyword to grab them from the outer scope.

My mistake was thinking “… from the outer scope.” GLOBAL doesn’t pull variables from “outer” scopes, it only pulls variables from the top-level, global scope.

As it turns out, a CodeIgniter view doesn’t run in the global scope. It gets swallowed by the CodeIgniter engine. It is evaluated and run from within a CodeIgniter scope, which in turn runs within the global scope. (I haven’t actually traced how many layers there are between a view and the global scope — there could be more.)

The only option in this case is to pass the variable to the function as a parameter.

The scope of a CodeIgniter view file is not obvious, nor could I find it mentioned in the documentation. Hopefully this post will save someone else the trouble of tracing why the GLOBAL keyword doesn’t seem to be working in CodeIgniter.

And if you have dug even deeper than me and can draw a scope diagram for CodeIgniter, let me know!

This entry was posted in Web programming and tagged , , , , . Bookmark the permalink.

4 Responses to A CodeIgniter view does not run in the global scope

  1. Alicia says:

    I had a similar problem. You can pass it to the function in the view loading process, but if you are loading views in views, I found that using the $GLOBAL variable was the best method to make sure I could use it where needed.

  2. Damchey says:

    Doesn’t $this->load->vars($data) in your controller work? That way the variables should be available in all views.

  3. noreplay says:

    the point is that you want to construct a function inside the view file and access that variable within the function

  4. you could assign the main CodeIgniter object to a variable and then use load->get_var($key) to get the view variable.

    function something() {
    $ci =& get_instance();
    $myvar = $ci->load->get_var(‘myvar);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s