Jump to content

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
https://forums.phpfreaks.com/topic/38345-solved-variables/
Share on other sites

Pass your variables as parameters instead.

 

function foo($foobar)
{
   echo '<b>' . $foobar . '</b>';
}

$bar = 'hello!';

foobar($bar);

 

If you have lots of variables to parse to the function then add them into an array instead.

Link to comment
https://forums.phpfreaks.com/topic/38345-solved-variables/#findComment-183781
Share on other sites

If $GLOBALS['variable1'] doesn't work, there is no way other than listing out your globals or passing them into the function as wildteen suggested. Function scope is there for a design reason and a technical reason, I'd suggest using OOP if you want functions that can share variables ;)

Link to comment
https://forums.phpfreaks.com/topic/38345-solved-variables/#findComment-183783
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
https://forums.phpfreaks.com/topic/38345-solved-variables/#findComment-183789
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
https://forums.phpfreaks.com/topic/38345-solved-variables/#findComment-183806
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.