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
Variable scope???
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Variable scope???
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)
Re: Variable scope???
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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Variable scope???
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)