0

hello i have a php file named index.php.

within that file two php scripts are there ...

they have to share four $variables values with each other...how coud i do this??

global,superglobal or static???
`

3
Contributors
3
Replies
18
Views
2 Years
Discussion Span
Last Post by ankit1122
1

If everything is contained within the same file, a static should be able to handle it. Without seeing your code it's difficult to tell. Just use some basic markup like:

class globals 
{
    static $var = "Hello world.";
}

echo globals::$var;

There's no reason why a global shouldn't, because if you're using classes you can call it in, and if you're not, a standard variable will be kept in memory throughout your script. Personally I find this a rather unorganised way to sort out your code though.

Finally, a superglobal is possilbe using the $GLOBALS variable to retrieve values. Again, it works but it's not very tidy.

So you're probably best in most cases to use a static, but thats only a generalised thing and in some cases I imagine one of the others might be better.

Votes + Comments
agreed with this
0

post some come please

when you say to scripts, do you mean 2 sections like <?php ?>
are you talking about sharing variables between functions

0

i have defined them in a php file and just using them in another php file after including that defining php file

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.