[PHP] Re: Include and require

2006-08-14 Thread Adam Zey

Dave Goodchild wrote:

Hi all - I have several require_once statements in my web app to load in
small function libraries. A common one bundles a variety of functions to
handle date math and map month numbers to month names. I originally defined
an array in that file plus a bunch of functions but when I loaded the page,
the array variable, referenced further down the page, was NULL. I wrapped a
function def around the array and returned it and all was fine.

I may be suffering from mild hallucinations, but can you not define
variables in a required file? It is not a scope issue as the array variable
is referenced in the web page, not in any function.



I know for a fact that you can define variables in PHP 4 and 5. The idea 
behind include and require is little more complex than copying and 
pasting the code. Many of my scripts include a config.php which has 
various variables created with setting information.


Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Include and require

2006-08-14 Thread Adam Zey

Dave Goodchild wrote:
I use a config file too. That was a sanity check. The file extract 
looked like this:


$months = array(1 = 'January', 2 = 'February', 3 = 'March', 4 = 
'April', 5 = 'May', 6= 'June',
7 = 'July', 8 = 'August', 9 = 'September', 10 = 
'October', 11 = 'November', 12 = 'December');


which was called in with require_once. The reference to $months in the 
calling page, checked with var_dump, was NULL.


When I wrapped it like this:

function getmonths() {

$months = array(1 = 'January', 2 = 'February', 3 = 'March', 4 
= 'April', 5 = 'May', 6= 'June',
7 = 'July', 8 = 'August', 9 = 'September', 10 = 
'October', 11 = 'November', 12 = 'December');

return $months;
}

it worked. Not sure why the simple variable didn't work.

On 14/08/06, *Adam Zey* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote:

Dave Goodchild wrote:
 Hi all - I have several require_once statements in my web app to
load in
 small function libraries. A common one bundles a variety of
functions to
 handle date math and map month numbers to month names. I
originally defined
 an array in that file plus a bunch of functions but when I
loaded the page,
 the array variable, referenced further down the page, was NULL.
I wrapped a
 function def around the array and returned it and all was fine.

 I may be suffering from mild hallucinations, but can you not define
 variables in a required file? It is not a scope issue as the
array variable
 is referenced in the web page, not in any function.


I know for a fact that you can define variables in PHP 4 and 5.
The idea
behind include and require is little more complex than copying and
pasting the code. Many of my scripts include a config.php which has
various variables created with setting information.

Regards, Adam Zey.




--
http://www.web-buddha.co.uk
http://www.projectkarma.co.uk 
Was the $months variable created inside an if statement or something 
else? PHP's rules of scope say that variables created inside a code 
block (like an if, a for, a while, a foreach), they stop existing the 
moment you exit that code block. So this:


$foo = bar;

if ( $foo == bar )
{
   $baz = narf;
}

echo $baz;

That code will output nothing, because $baz is empty by the time I try 
to output it. The solution that I use is this:


$foo = bar;
$baz = ;

if ( $foo == bar )
{
   $baz = narf;
}

echo $baz;


In which case the output would be narf, because the variable existed 
before I changed it in the if. This sounds like it might be your 
problem, though I can't know without seeing the code.


Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Include and require

2006-08-14 Thread Chris

Adam Zey wrote:

Dave Goodchild wrote:
I use a config file too. That was a sanity check. The file extract 
looked like this:


$months = array(1 = 'January', 2 = 'February', 3 = 'March', 4 = 
'April', 5 = 'May', 6= 'June',
7 = 'July', 8 = 'August', 9 = 'September', 10 = 
'October', 11 = 'November', 12 = 'December');


which was called in with require_once. The reference to $months in the 
calling page, checked with var_dump, was NULL.


When I wrapped it like this:

function getmonths() {

$months = array(1 = 'January', 2 = 'February', 3 = 'March', 4 
= 'April', 5 = 'May', 6= 'June',
7 = 'July', 8 = 'August', 9 = 'September', 10 = 
'October', 11 = 'November', 12 = 'December');

return $months;
}

it worked. Not sure why the simple variable didn't work.

On 14/08/06, *Adam Zey* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote:

Dave Goodchild wrote:
 Hi all - I have several require_once statements in my web app to
load in
 small function libraries. A common one bundles a variety of
functions to
 handle date math and map month numbers to month names. I
originally defined
 an array in that file plus a bunch of functions but when I
loaded the page,
 the array variable, referenced further down the page, was NULL.
I wrapped a
 function def around the array and returned it and all was fine.

 I may be suffering from mild hallucinations, but can you not define
 variables in a required file? It is not a scope issue as the
array variable
 is referenced in the web page, not in any function.


I know for a fact that you can define variables in PHP 4 and 5.
The idea
behind include and require is little more complex than copying and
pasting the code. Many of my scripts include a config.php which has
various variables created with setting information.

Regards, Adam Zey.




--
http://www.web-buddha.co.uk
http://www.projectkarma.co.uk 
Was the $months variable created inside an if statement or something 
else? PHP's rules of scope say that variables created inside a code 
block (like an if, a for, a while, a foreach), they stop existing the 
moment you exit that code block. So this:


$foo = bar;

if ( $foo == bar )
{
   $baz = narf;
}

echo $baz;

That code will output nothing, because $baz is empty by the time I try 
to output it.


That's wrong sorry :)

$ php -a
Interactive mode enabled

?php
$foo = bar;

if ( $foo == bar )
{
   $baz = narf;
}

echo $baz;
narf

Works fine.

If you only create the variable inside the if you won't be able to use 
it if the code doesn't get into the if (it'll be an undefined variable):


$ php -a
Interactive mode enabled

?php
error_reporting(E_ALL);
if (1 == 0) {
  $foo = blah;
}
echo $foo;

Notice: Undefined variable:  foo in - on line 6

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: include (or require) doesn't seem to work

2004-05-12 Thread Daniel Barbar
Thanks Torsten! Using the file-system relative path made it work (I had
tried only with the absolute path, which ddidn't work). However, I checked
again and I did have 'allow_url_fopen = On' in /etc/php.ini. I'll take a
closer look later and report the problem if I find it. Thanks again,

Daniel

Torsten Roehr [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
  Try including it as a local file:
  require_once library.php'; // if it is in the same directory as the file
 you

 Forgot a quote here, sorry:
 require_once 'library.php';

 Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: include (or require) doesn't seem to work

2004-05-12 Thread Torsten Roehr
Daniel Barbar [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Thanks Torsten! Using the file-system relative path made it work (I had
 tried only with the absolute path, which ddidn't work). However, I checked
 again and I did have 'allow_url_fopen = On' in /etc/php.ini. I'll take a
 closer look later and report the problem if I find it. Thanks again,

 Daniel

Using the absolute path works as well - but you have to use the full LOCAL
file path, e.g. /htdocs/www/your-domain etc.

You can get this value from $_SERVER['DOCUMENT_ROOT'].

Regards, Torsten


 Torsten Roehr [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
   Try including it as a local file:
   require_once library.php'; // if it is in the same directory as the
file
  you
 
  Forgot a quote here, sorry:
  require_once 'library.php';
 
  Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: include (or require) doesn't seem to work

2004-05-11 Thread Torsten Roehr
Daniel Barbar [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Hi,

  I'm almost ashamed to ask this question as surely enough the
 problem is something very basic but, nonetheless, I can't put my finger on
 it. I'm trying to implement the concept of a library (library.php) on PHP
 where I define (once) all auxiliary functions and then use them in a file
 (for instance index.php) via the 'require' or 'include' constructs. Here's
a
 reduced version of what I'm doing:



 index.php:

 ?php
 $lang = (isset($_REQUEST['lang']) ? $_REQUEST['lang'] : es);
 echo index.php: include_path is  . (ini_get('include_path')).br;
 require(http://tristan/library.php?lang=$lang;);
 my_function(en);
 ?



 library.php:

 ?php
 echo library.php: Called with $_SERVER[HTTP_HOST]:/$_SERVER[REQUEST_URI]
 br;
 function my_function($lang = es) {
 echo my_function() says $lang;
 }
 echo library.php: loadedbr;
 ?



 When I load index.php I get the following:



 index.php: include_path is .:/usr/local/php/4.3.6/lib/php
 library.php: Called with tristan://library.php?lang=es
 library.php: loaded



 Fatal error: Call to undefined function: my_function() in
 /www/htdocs/index.php on line 5



 It seems that the name space on index.php never gets updated
 with the function definitions made on library.php. What am I doing wrong?
 Thanks! Cheers,



 Daniel

Hi Daniel,

take a look at this section of the manual about including remote files:


If URL fopen wrappers are enabled in PHP (which they are in the default
configuration), you can specify the file to be included using a URL (via
HTTP or other supported wrapper - see Appendix J for a list of protocols)
instead of a local pathname. If the target server interprets the target file
as PHP code, variables may be passed to the included file using a URL
request string as used with HTTP GET. This is not strictly speaking the same
thing as including the file and having it inherit the parent file's variable
scope; the script is actually being run on the remote server and the result
is then being included into the local script.


Try including it as a local file:
require_once library.php'; // if it is in the same directory as the file you
have this line in

Also you don't have to pass $lang via Get to the file as the included code
will have access to $lang as it is then in the scope of the script.

Regards, Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: include (or require) doesn't seem to work

2004-05-11 Thread Torsten Roehr
 Try including it as a local file:
 require_once library.php'; // if it is in the same directory as the file
you

Forgot a quote here, sorry:
require_once 'library.php';

Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: include() and require() problem

2002-04-19 Thread Michael Virnstein

be sure that the path to the include file is relative to the script that was
requested
by the user. that's important if you include include-files into included
files.
you then have to make sure that the path to your include-file in the
include-file is
relative to the file that included the file, in which the include-file is
included.
*lol* what a sentence. anyway, hope you get my meaning!
another problem is using include /myincfiles/myfile.inc;
this way it could be searched in the root of the server, where for sure
your file can't be found. use relative path instead:
include ./myincfiles/myfile.inc;
./ means the same directory.
or use an absolute path, which has to contain the whole path from the
server's root, e.g.:
include /wwwroot/somedir/yourdocroot/myincfiles/myfile.inc;

Regards Michael

Rodrigo Peres [EMAIL PROTECTED] schrieb im Newsbeitrag
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 List,

 I'm trying to include some .inc files in my project, but I got errors
 all time saying that this includes couldn't be found. I'm a newbie, so I
 can't understand what's happend, since it worked pretty good in my local
 machine and not in the ISP. I tried to speak to ISP many times but they
 couldn't solve to.
 My directorie structure is : HRM(directorie)-includes(directorie)-my
 inc files(to be included in files that is in the directorie HRM).

 Thank's

 Rodrigo




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: include() and require() problem

2002-04-19 Thread Vins

I'm also having a problem with including file into and include file.
my script reads.

( (strpos($PHP_SELF,admin/)) ? include('../includes/mysql.inc.php') :
include('includes/mysql.inc.php') );
mysql_pconnect($mysqlDBhost,$mysqlDBuser,$mysqlDBpass)
 or die (Database access error. Please contact the site webmaster at
$webmasterEmail.);
mysql_select_db($mysqlDBdatabase)
 or die (Couldn't Select Database. Please contact the site webmaster at
$webmasterEmail.);

my server is running php4.1.2 and there is not problem
i am also running php4.1.2 but reads back a problem.
this one

Warning: Failed opening 'includes/mysql.inc.php' for inclusion
(include_path='c:\php4\pear') in
D:\Apache\htdocs\auction4sale\includes\config.inc.php on line 47
Database access error. Please contact the site webmaster at .

any suggestions ?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php