Problem including files in PHP

They have: 51 posts

Joined: Dec 2002

<?php
echo 'trying to include testInc.php...<br>';
include(
'testInc.php');
echo
'<br><br>trying to include testInc.php?some=thing...<br>';
include(
'testInc.php?some=thing');
?>
'

that code is within the file something.php
testInc.php contains only an echo saying 'done'.

the first include (testInc.php) works fine but the second (testInc.php?some=thing) doesn't work. When I set them to require() the second require produces this error.

Fatal error: main(): Failed opening required 'testInc.php?some=thing' (include_path='.:/usr/lib/php') in /var/www/localhost/rules/technology/ships/something.php on line 5

any idea whats up?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

What is up is it is not finding a filename "testInc.php?some=thing" Filesystems do not take query strings like URL's do.

To do what it appears you are trying to do, you could do the following:

-- Main File --

<?php
 
echo \"Going to use a variable named Some that equals Thing<br>\n\";
 
$Some = \"Thing\";
  include('testInc.php');
?>

-- testInc.php --
<?php
 
echo \"The value of Some is \" . $Some . \"<br>\n\";
?>

When you do an include, it takes the code of the include executes it as if it is part of the same file, so as it processes your script, it sees:

<?php
 
echo \"Going to use a variable named Some that equals Thing<br>\n\";
 
$Some = \"Thing\";
  echo \"The value of Some is \" .
$Some . \"<br>\n\";
?>

For more information, see http://www.php.net/include

-Greg

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Whatever it is you're doing, it might be a good idea, if you're trying to modularise it, to write it with OOP principles. In testInc, write your processes as a class, and in the main file, just initialise the class and pass whatever values you need.

They have: 51 posts

Joined: Dec 2002

cheers guys, looks like it was just some lax configuration on the old server the allowed it to work like that.

Abhishek,
yeh, we've had a redesign of the system along OOP lines on the books for a while now but we're a volunteer project so things like that tend to get shoved to the back. Looks like we're gonna have to implement it now though Smiling

cheers for your help guys.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.