Re: php forgetting variables very easily
| From: | Ernest E Vogelsinger | Date: | Tue, 04 Mar 2003 19:00:05 +0000 |
| Subject: | Re: php forgetting variables very easily | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-138151@lists.php.net to get a copy of this message | ||
At 19:37 04.03.2003, Ian A. Gray spoke out and said:
--------------------[snip]--------------------
>Hi everyone.I am probably doing something obviously
>wrong but I can't seem to sort it! It's regarding
>variables.In one script, lets call it main.php I make
>a variable- say $colour. It inludes a file which
>prints the variable:
>
>main.php
><?php
>$colour = 'green';
>include('new.php')
>?>
>
>new.php
><?php
>echo $colour
>?>
>
>Ok, this works fine for me. However it doesn't seem
>to work when main.php and new.php have html in them.
>The variable seems to be lost and new.php prints
>nothing. Has anyone got any words of wisdom on this?
>It may help if I actually showed the two pages here,
>but they are long and I thought it would be a waste of
>space.
--------------------[snip]--------------------
in main.php, try to make $colour global:
main.php
<?php
global $colour;
$colour = 'green';
include('new.php')
?>
new.php
<?php
global $colour;
echo $colour;
?>
This seems to me like an inconsistency within PHP as to when variables are
"automagically" available, and when you have to declare them global.
However you should avoid using globals anyway...
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/