473,573 Members | 3,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing variables outside a function... possible?

Hi,

Can someone tell me if the following is possible?

I have a file containing some STATIC variables, and some functions e.g.
-------------------------------------------------
$ARCHIVE_INPUT_ FILES = false;
$ARCHIVE_OUTPUT _FILES = false;
$ARCHIVE_CONFIG _FILES = false;
$HTML_BODY_BACK GROUND = "gfx/gradient.gif";

function firstOne
{
// Do something e.g.
echo $HTML_BODY_BACK GROUND;
}
-------------------------------------------------

The functions CANNOT see the variables I have declared at the top, so is
there anything I can do make the functions see the variables i.e.
declare them differently?

Thanks!

- Lee

Jul 17 '05 #1
3 15251
In article <3f**********@n ews3.es.net>,
Leigh Riley <do************ *****@home.org> wrote:
function firstOne
{
// Do something e.g.
echo $HTML_BODY_BACK GROUND;
}
-------------------------------------------------

The functions CANNOT see the variables I have declared at the top, so is
there anything I can do make the functions see the variables i.e.
declare them differently?


function firstOne {
global $HTML_BODY_BACK GROUND;
echo $HTML_BODY_BACK GROUND;
}
or declare them as constants:

define('HTML_BO DY_BACKGROUND', 'gfx/gradient.gif');

function firstOne {
echo HTML_BODY_BACKG ROUND; // no $ sign == constant
}
JP

--
Sorry, <de*****@cauce. org> is een "spam trap".
E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.
Jul 17 '05 #2

On 25-Sep-2003, Leigh Riley <do************ *****@home.org> wrote:
Can someone tell me if the following is possible?

I have a file containing some STATIC variables, and some functions e.g.
-------------------------------------------------
$ARCHIVE_INPUT_ FILES = false;
$ARCHIVE_OUTPUT _FILES = false;
$ARCHIVE_CONFIG _FILES = false;
$HTML_BODY_BACK GROUND = "gfx/gradient.gif";

function firstOne
{
// Do something e.g.
echo $HTML_BODY_BACK GROUND;
}
-------------------------------------------------

The functions CANNOT see the variables I have declared at the top, so is
there anything I can do make the functions see the variables i.e.
declare them differently?


declare them as global in the funciton

global $ARCHIVE_INPUT_ FILES;

http://www.php.net/manual/en/languag...bles.scope.php

--
Tom Thackrey
www.creative-light.com
Jul 17 '05 #3
[ not posted to alt.php ]

Leigh Riley wrote:
Hi,

Can someone tell me if the following is possible?

I have a file containing some STATIC variables, and some functions e.g.
-------------------------------------------------
$ARCHIVE_INPUT _FILES = false;
$ARCHIVE_OUTPU T_FILES = false;
$ARCHIVE_CONFI G_FILES = false;
$HTML_BODY_BAC KGROUND = "gfx/gradient.gif";

function firstOne
{
// Do something e.g.
echo $HTML_BODY_BACK GROUND;
}
-------------------------------------------------

The functions CANNOT see the variables I have declared at the top, so is
there anything I can do make the functions see the variables i.e.
declare them differently?


Well ... if they're STATIC variables make them constants :-)
That way you can see them inside the functions

<?php
define('ARCHIVE _INPUT_FILES', false);
define('ARCHIVE _OUTPUT_FILES', false);
define('ARCHIVE _CONFIG_FILES', false);
define('HTML_BO DY_BACKGROUND', 'gfx/gradient.gif');
?>
Failing that you can declare the variables global in the function:
<?php
function firstOne
{
global $ARCHIVE_INPUT_ FILES, $ARCHIVE_OUTPUT _FILES;
global $ARCHIVE_CONFIG _FILES, $HTML_BODY_BACK GROUND;

// Do something e.g.
echo $HTML_BODY_BACK GROUND;
}
?>

or maybe access the $GLOBAL array inside the function
<?php
function firstOne
{
// Do something e.g.
echo $GLOBALS['HTML_BODY_BACK GROUND'];
}
?>

Happy Coding :-)
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #4

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

Similar topics

8
2342
by: [RaZoR] | last post by:
hello, main script creates IE window, put an array there and then calls a child script. chils script makes an array element equal to some object and returns control to main script. then main script doesn't see that object and its functions. why? here is the code I implemented: //in main script
7
3116
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 */
5
2691
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of properties. I want to know if there is a way to access the methods and the properties of the Owner class for the class that's inside it? I.e. I...
12
11703
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very particular and involves automatically generated code. I know several other ways of attacking my problem, but this would be the cleanest if it could be...
89
5977
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
55
6182
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
2
2542
by: Licheng Fang | last post by:
On Apr 14 2003, 10:30 pm, Alex Martelli <al...@aleax.itwrote: Sorry to dig up this old thread, but I would like to know what's the rationale is. Why can't a nested function rebind a variable of an outer function? I was trying to write a function that creates another function and returns it when I came across this problem. These two...
1
29324
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this...
0
8200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7773
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...
0
8061
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6411
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5584
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...
0
5289
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...
0
3730
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...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1038
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...

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.