0

To quickly summarize my problem, I simply copied the example of (http://php.net/manual/en/language.variables.scope.php) into one of my views and wondered why it was nothing echoed on my screen.

Here the example of php.net:

<?php
$a = 1;
$b = 2;

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

    $b = $a + $b;
} 

Sum();
echo $b;
?>

I expected '3' on my screen but $b returned still '2'.. Well I tested this behavior about 3 hours with different examples and it seems that kohana does some tricky things. I thought Kohana only extracts the View::_data into the local scope and everything would be ok but now I have no clue about it.

Could somebody explain me that behaviour?

How can I wrap PHP legacy code in Kohana? couldn't help me..

Btw, sorry for my language mistakes!

UPDATE:

I know using globals is bad but my clean approach didn't work yesterday.. But now I don't know why everything works again, maybe there was a caching problem.

But besides that I understood the mistake of my given example, thank you. Seems that it has nothing to do with output buffering..

4

1 回答 1

3

您可能不在全球范围内。所以所有的变量都应该在前面加上globallike

<?php
global $a = 1;
global $b = 2;

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

    $b = $a + $b;
} 

Sum();
echo $b;
?>

PS:全局变量是万恶之源。没有它们,一切都可以而且应该实现。

于 2012-09-10T23:27:39.747 回答