Jump to content

[SOLVED] Variables


Azu

Recommended Posts

Please tell me what is a simple way to make all variables global everywhere? So that when I declare a variable somewhere in my script it will work everywhere without me having to write "global $variablea,$variable1,$variablex,$variableblablablabla,$grrrrr,$zomg,$wtf,$asdf,$varrrriiibbbllle;" in every single function which is annoying as hell?

 

Oh and I've tried using the $globals array but it doesn't work on my server so I can't use that.

 

 

P.S. my arrays and objects also need to work everywhere

Link to comment
Share on other sites

Every variable that is defined is put into the $GLOBALS superglobal array and can be referenced that way:

<?php
function somefunc($var) {
echo "Passed variable: $var<br>";
echo 'Global var $somevar = ' . $GLOBALS['somevar'] . '<br>';
}

$somevar = 'This is a test';
somefunc('This is a passed value');
?>

 

Ken

Link to comment
Share on other sites

Isn't there a way to just remove the function scope or whatever? And what is "OOP"?

 

OOP = object oriented programming

 

And no, you can't just remove function scope, every running function is added to the stack and that function's frame holds it's own variables, you cant seperate the variables from the stack like you want unless you use OOP where the instance variables are stored in the heap...

 

This is assuming I remembered everything correctly and PHP does it like java/c/other languages (correct please jesi? I probably said something wrong)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.