CodeIgniter Forums
Variable scope and functions in view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Variable scope and functions in view (/showthread.php?tid=55316)



Variable scope and functions in view - El Forum - 10-20-2012

[eluser]Leo78[/eluser]
Hey,

First of all, I know that the following thing it's unacceptable, but I don't have any choice, so don't tell me about it.

I tried to write this simple thing in the view (taken from PHP.net just for the example):

Code:
// view_test.php
<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
}

Sum();
echo $b;
?>

The controller is simple too:

Code:
class Draw extends MY_Controller {

    function test()
    { $this->load->view('view_test'); }
}

The result should be $b = 3, but $b = 2 and it doesn't refer to the `global` attitude, why does it happen and what can I do to get it works?

Thank you!


Variable scope and functions in view - El Forum - 10-20-2012

[eluser]solid9[/eluser]
Well, It works for me.
http://coder9.com/php/global_var.php

What PHP version you are using?




Variable scope and functions in view - El Forum - 10-20-2012

[eluser]Leo78[/eluser]
[quote author="solid9" date="1350720120"]Well, It works for me.
http://coder9.com/php/global_var.php

What PHP version you are using?

[/quote]

I'm talking about test it in view file (of CodeIgniter).


Variable scope and functions in view - El Forum - 10-20-2012

[eluser]Aken[/eluser]
What are you actually trying to do? There has to be a far better solution than defining a function in the middle of your view (which is ugly).


Variable scope and functions in view - El Forum - 10-20-2012

[eluser]Leo78[/eluser]
[quote author="Aken" date="1350723355"]What are you actually trying to do? There has to be a far better solution than defining a function in the middle of your view (which is ugly).[/quote]

You are absolutely right, the problem is that there is nothing to do. I'm trying to integrate a really ugly-code system which contains something like 20 files.