473,788 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can you declare global scope in an included file and see those variables throughout other code?

I have an file I call using:
require_once()

In this file I have variables I'd like to use in the calling page and
functions called by that page.

How can I do this?

example:

<page1.php>
require_once(co de1.php);
require_once(ut ilities.php);

Function simple() {
$varA = $code1_var_A + $code1_var_B;
return varA;
}
Jul 17 '05 #1
5 2922
On Fri, 27 Aug 2004 17:26:10 -0400, "NotGiven" <no****@nonegiv en.net> wrote:
I have an file I call using:
require_once ()

In this file I have variables I'd like to use in the calling page and
functions called by that page.

How can I do this?

example:

<page1.php>
require_once(c ode1.php);
require_once(u tilities.php);

Function simple() {
$varA = $code1_var_A + $code1_var_B;
return varA;
}


Look up the 'global' statement.

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
> In this file I have variables I'd like to use in the calling page and
functions called by that page.


You can use the a variable globally by using the superglobal array called
$GLOBALS instead of the keyword global.
Check here for more information: http://us4.php.net/language.variables.scope

There is another way, and this method is one I prefer personally.
You can use the define keyword to define a constant. Constants defined this
way are automatially global, so you can access them from included scripts
and functions as well.

define('MYVARIA BLE', 'stringvalue');

print MYVARIABLE;

http://us4.php.net/define

If you know you will not need to change the value you should use
constants... if you have a reason to modify the value of the variable you
should use the $GLOBALS array.

_______________ _______________ ______
Wil Moore III, MCP | Integrations Specialist
Jul 17 '05 #3
"laidbak69" wrote:
In this file I have variables I’d like to use in the calling page and
functions called by that page.


You can use the a variable globally by using the superglobal array
called
$GLOBALS instead of the keyword global.
Check here for more information:
http://us4.php.net/language.variables.scope

There is another way, and this method is one I prefer personally.
You can use the define keyword to define a constant. Constants

defined this
way are automatially global, so you can access them from included
scripts
and functions as well.

define(’MYVARIA BLE’, ’stringvalue’);

print MYVARIABLE;

http://us4.php.net/define

If you know you will not need to change the value you should use
constants... if you have a reason to modify the value of the variable you
should use the $GLOBALS array.

_______________ _______________ ______
Wil Moore III, MCP | Integrations Specialist


I have had problems getting $GLOBALS to work reliably for me when
defined deep. I therefore prefer to use constants when declaring a
variable deep in a function, or in an include.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-declare-...ict144180.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=482423
Jul 17 '05 #4
what is happening now is this. I definemy constants in code1.php but I can
not reference them in my main code.

<code1.php>
//define my constant
define("mName", "My name");
<end of page>

<page1.php>
require_once(co de1.php);
echo mName;
//prints "mName" - should print "My name"
What do I change - Many many thanks!


<la*******@hotm ail.com> wrote in message
news:10******** *****@corp.supe rnews.com...
In this file I have variables I'd like to use in the calling page and
functions called by that page.
You can use the a variable globally by using the superglobal array called
$GLOBALS instead of the keyword global.
Check here for more information:

http://us4.php.net/language.variables.scope
There is another way, and this method is one I prefer personally.
You can use the define keyword to define a constant. Constants defined this way are automatially global, so you can access them from included scripts
and functions as well.

define('MYVARIA BLE', 'stringvalue');

print MYVARIABLE;

http://us4.php.net/define

If you know you will not need to change the value you should use
constants... if you have a reason to modify the value of the variable you
should use the $GLOBALS array.

_______________ _______________ ______
Wil Moore III, MCP | Integrations Specialist

Jul 17 '05 #5
On Sat, 28 Aug 2004 07:32:24 -0400, "NotGiven" <no****@nonegiv en.net> wrote:
what is happening now is this. I definemy constants in code1.php but I can
not reference them in my main code.

<code1.php>
//define my constant
define("mName" , "My name");
<end of page>

<page1.php>
require_once(c ode1.php);
This isn't right for starters, it'll try and include a file 'code1php' and
should emit two warnings.

(use of undefined constant code1, assumed string, same again for php, then
it'll concatenate the two with the '.' operator between then).

Put the filename in quotes.
echo mName;
//prints "mName" - should print "My name"
Again, 'use of undefined constant mName, assumed string'.
What do I change - Many many thanks!


Set error_reporting to E_ALL and make sure display_errors is on - you're
coding blindfolded at the moment!

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6722
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for the sake of convenience (surely we learned this much from basic). here's my proposal: all "global" (document scope) variables must be declared by 'var' outside a function block.
5
3222
by: A | last post by:
Hi, Consider this code: //Header File - Foo.h int i = 0; // non-static global variable class Foo{ ...
9
2381
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any function at the top of the a file where you have them. //Tony
6
19062
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the external file, but I keep getting an error about the object does not exist. Can someone tell me where or how to declare a global variable in an external file that is available after the page is loaded.
5
3495
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file scope. If you have a variable whose scope is file scope in another translation unit, you have to provide a local declaration to access that variable from the other translation unit. Also, I don't see "global variable" used once in the standard....
11
2569
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
7
3146
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
41
10678
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
44
3647
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.