Welcome Guest, Not a member yet? Register   Sign In
Variable scope and functions in view
#1

[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!
#2

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

What PHP version you are using?

#3

[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).
#4

[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).
#5

[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.




Theme © iAndrew 2016 - Forum software by © MyBB