473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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_BACKGROUND = "gfx/gradient.gif";

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

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 15243
In article <3f**********@news3.es.net>,
Leigh Riley <do*****************@home.org> wrote:
function firstOne
{
// Do something e.g.
echo $HTML_BODY_BACKGROUND;
}
-------------------------------------------------

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_BACKGROUND;
echo $HTML_BODY_BACKGROUND;
}
or declare them as constants:

define('HTML_BODY_BACKGROUND','gfx/gradient.gif');

function firstOne {
echo HTML_BODY_BACKGROUND; // 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_BACKGROUND = "gfx/gradient.gif";

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

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_OUTPUT_FILES = false;
$ARCHIVE_CONFIG_FILES = false;
$HTML_BODY_BACKGROUND = "gfx/gradient.gif";

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

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_BODY_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_BACKGROUND;

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

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

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
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...
7
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...
5
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...
12
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...
89
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...
55
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...
2
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...
1
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.