Jump to content

Recommended Posts

I have a config.php at the top of both pages that contains (amongst other code)

<?php $boxselected=$_GET['b']; ?>

 

Now in the files I use $boxselected for a few different purposes but I never change it. (ie it always stays the same info passed in the address bar as ?b=example)

 

However, only in one of the files does $boxselected worked. HOWEVER other lines of config.php  ARE working in BOTH pages.

 

Is there any code that would disable $_GET that might be interfering?

 

The page on which $boxselected does NOT work uses a function contained in config.php. But I don't see why that would affect $_GET.

 

And I've checked spelling/ typos so please don't suggest that -_-

What value does $boxselected end up with?

 

You likely have some code on that page that is overwriting $boxselected, such as -

 

if($boxselected = something)

 

rather than

 

if($boxselected == something)

 

The first example sets $boxselected to something and then tests the resulting value. The second example tests if $boxselected is equal to something.

^ But it IS blank when used in the function I made, even though $boxselected is defined before the function

 

EDIT: I redefined  $boxselected inside the function and it works. Is there any other way to do it without redefining the same thing twice?

what do you mean by

However, only in one of the files does $boxselected worked. HOWEVER other lines of config.php  ARE working in BOTH pages.
Do you send the values of config.php to $_GET array?

It is always a good practice to print this line:

echo nl2br(print_r($_GET,1));

Where you can see the whole array $_GET and check its keys and values.

 

Is there any other way to do it without redefining the same thing twice?

just add the beginning of the function "global $boxselected;" or make "$boxselected" one of the parameters passed to the function.

Please don't recommend using the global keyword to being values into a function. Could you imagine what a mess php would be like as a programming language if all of the built in functions did that and you had to keep track of which values were used by which function because they would all need to be uniquely named so that you could use more than a few functions without conflict?

 

Only bring values into functions by passing them in as parameters so that you maintain the general purpose nature and encapsulation that functions are supposed to provide.

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.