Jump to content

invalid require path error if i use && in if/else logic


Recommended Posts

Hi sorry for the trouble guys but i'm really stuck on this one, and i've tried everything i can. (I've been using php only for sometime now)

 

I'm stuck at this code and can't figure out why this is failing.

 

Objective of the script :

To try and include BOTH files or throw and exception if even one fails.

 

I tried :

Including just one at a time. Its fine, so i know the path is correct.

I tried a if (! (expr1 && expr2) )  statement to check if the syntax is correct to create a everything or nothing logic.

 

$offlineLibraryPath = '/var/www/common/lib';
// Attempt to include the files
try {
if(!
    (	
    	require_once($offlineLibraryPath.'/variables/all.php') &&
    	require_once($offlineLibraryPath.'/functions/all.php')
    )
  ) 
     { throw new Exception ('Including all the common library files failed.'); }
} catch (Exception $e) {
echo '<b>Error</b> :'.$e->getMessage();
}

 

I get this error :

Warning: require_once(1): failed to open stream: No such file or directory in /var/www/common/lib/startupIncludes.php on line 18 Fatal error: require_once(): Failed opening required '1' (include_path='/var/www/common/lib/: /var/www/culturesque/lib/: /var/www/mine/lib/') in /var/www/common/lib/startupIncludes.php on line 18

 

 

Thanks in advance for the help!

@ManiacDan : I need the require to happen successfully or return an error. If i use file_exists, won't i need to use a require after that to include the file (bringing us back to check if the require will happen successfully).

 

Ahh, i get it now , all this while echo include(filename); would always return 1, fooling me into thinking its the boolean return, when its actually the return value in filename. Adding a return false in the included file proved this for me. Thanks thorpe :D

It doesn't seem like you understand what the include/require functions actually do.  When you include a PHP file, basically what happens is PHP copies the contents of the file right into the script that's including them.  Include/require will ALWAYS succeed if the file is present, the only question you should have is whether or not the PHP inside the included file is valid.  This is something you have to ensure by writing good code in your include files, not by wrapping them in a validator.

 

Include files shouldn't "return" anything to the parent file, because they're designed to operate as if the code is IN the parent file to begin with.

 

-Dan

It doesn't seem like you understand what the include/require functions actually do.  When you include a PHP file, basically what happens is PHP copies the contents of the file right into the script that's including them.  Include/require will ALWAYS succeed if the file is present, the only question you should have is whether or not the PHP inside the included file is valid.  This is something you have to ensure by writing good code in your include files, not by wrapping them in a validator.

 

Include files shouldn't "return" anything to the parent file, because they're designed to operate as if the code is IN the parent file to begin with.

 

-Dan

 

yep yep, i get it now, all this while i mistook require/include for a function because of the () that it uses.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.