Jump to content

Recommended Posts

I have a function which is used several times in my site, but it also needs access with a ton of variables, some of which change sometimes. Rather than going through and saying "global $var1, $var2, $var3" a hundred times, is there a way for me to make all variables usable in the function, preferably without using $GLOBALS?

 

That way, I could do something such as:

 

<?php

//define variables
$cows= 2;

//declare moo function
function moo()
{
//display content
echo $cows;
}

//call moo function
moo();

?>

 

Which, if worked, would display "2".

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/149275-solved-globals/
Share on other sites

But wouldn't that be worse than globals? You'd have to define all of the sessions, then delete them all at the end (or the user will have hundreds of cookies on their computer [i think]).

 

If I try to pass them through parameters, the declarations of the functions would be extremely confusing and tedious, like:

 


test($var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1, $var1);

 

Of course, they wouldn't all be "$var1", but it is pointless to go through this post and give them all a unique name, cause it's an example.

Link to comment
https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783916
Share on other sites

Put the variables into an array and pass them in within 1 parameter.

Also why would you need so many variables within a function?

 

Functions are for taking in data and returning a result. This could be a new value or an array of values. They are not designed and should hardly ever be used to modify the values of variables outside the scope of the function (for this job you would pass variables by reference). You will end up with unexpected results on your page without knowing the cause.

Link to comment
https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783922
Share on other sites

But like I've said, I change variables quite often. Going through a huge array or several declarations can be very tedious, cause errors, etc.

 

What about including the file, rather than using a function?

 

So, instead of:

 

<?php

//define variables
$var="blah";

//declare moo function
function moo()
{
//display content
echo $var;
}

//call moo function
moo();

?>

 

I could do:

 

<?php

//define variables
$moo="blah";

//include moo file
include("moo.php");

?>

 

moo.php would be:

 

<?php

//display content
echo $moo;

?>

 

My worry is, though, that it would not be as efficient as using a function (like, it would increase filesize or something). Is that true?

Link to comment
https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783926
Share on other sites

The more includes there will be a slight performance hit, but for the most part your overall parsed file size will be roughly the same. Still I would recommend using arrays and sessions, both will make your job a lot easier, particularly the sessions as you can pass all of that data quickly and efficiently as well as remembering it for use later on in the RPG.

Link to comment
https://forums.phpfreaks.com/topic/149275-solved-globals/#findComment-783931
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.