Variable scope???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
badmaash
Forum Newbie
Posts: 2
Joined: Tue Jan 29, 2008 1:01 pm

Variable scope???

Post by badmaash »

I am doing a bit of PHP coding with a shopping cart software.

I have noticed that they have one file with a list of definition such as:

define('DATE_FORMAT', 'd/m/Y');

but this constant DATE_FORMAT I have seen being used by another php file without reference to the php file that defined it. So there was no require or include code line for the php file that defined DATE_FORMAT.

Does this mean that contants defined in one file are available to all other scripts without having to reference the file that defined the variable?

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variable scope???

Post by Christopher »

No, but the variable has to be define within the request at some point before it is used. So a script may not include that file, but it is included by a file that did include that file. If it had not been included at some point before that code runs, you would get an error.
(#10850)
badmaash
Forum Newbie
Posts: 2
Joined: Tue Jan 29, 2008 1:01 pm

Re: Variable scope???

Post by badmaash »

So you are saying that as long a one file has made use of this variable any other file/script can then make use of without referencing the file that it was created in?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variable scope???

Post by Christopher »

It is not a matter of "made use of". If the define() is in a script that is included before the code that uses it -- it will be available. You don't have to include it directly. When you include a file, PHP adds it to everything else that has been included in that request.
(#10850)
Post Reply