1

I have a code snippet as follows

echo "calling require";
require ('../includes/functions/database.php');
echo 'require called';

my code echoes the first command and prints "Calling required."

as soon as it hits require function my code breaks and does not execute any further, as a result I don't get to print the last echo "require called"

What could be the possible reason?

6
  • by the way I have confirmed the existence of file at the location I have specified in require parameter. Commented Jan 17, 2012 at 9:05
  • Any exit() in your database.php file ? Can you show us this file ? Commented Jan 17, 2012 at 9:06
  • 'If you use the require directive to load a file, your script will die if the file is not found or the user that is running the web server does not have read access to it.' --php.net Check your permissions Commented Jan 17, 2012 at 9:07
  • add error_reporting(E_ALL); ini_set('display_errors', 'On'); above the line that causes the script to halt and post the output. Commented Jan 17, 2012 at 9:09
  • Check that the current working directory (use getcwd()) is where you think it is... it isn't always the location where the file was loaded. Commented Jan 17, 2012 at 9:37

5 Answers 5

4

Let php show error messages and you will find the error pretty quick.

Add these lines before the require

ini_set('display_errors', true);
error_reporting(E_ALL);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks remy, i was able to see and then fix the error by using error_reporting(E_ALL); ini_set('display_errors', 'On'); above the rogue line. thanks for your input.
0

The required file:

  • doesn't exist
  • exists, but your relative path doesn't match it's location
  • is not readable
  • has syntax errors itself

Any of those can be the reason.

2 Comments

Btw, downvoting other people's answers doesn't make yours better.
thanks narf, i was able to see and then fix the error by using error_reporting(E_ALL); ini_set('display_errors', 'On'); above the rogue line. thanks for your input. i agree with your stance buddy on downvoting..:)
0

Requiring a file in PHP by using relative path must be prohibited. The relative path depends on the PHP file being called first by the browser. ALways require or include files using absolute path.

1 Comment

thanks jerome, i was able to see and then fix the error by using error_reporting(E_ALL); ini_set('display_errors', 'On'); above the rogue line. thanks for your input.
-1

The file may exist but may be unreadable. Use is_readable() to check it:

if(is_readable('../includes/functions/database.php')) {
   echo('ok!');
}

Also, there may be an error or die() inside database.php file which prevents further execution. Check database.php for errors and die / exit calls.


Add error_reporting(E_ALL); ini_set('display_errors', 'On'); at the beginning of your scipt to debug errors as Salman A suggested in the comments.

1 Comment

thanks kdzwinel, i was able to see and then fix the error by using error_reporting(E_ALL); ini_set('display_errors', 'On'); above the rogue line. thanks for your input.
-1

If you just want your code to be executed no matter if the require did work, you could use include instead of require as it generates a warning instead of a fatal error.

1 Comment

thanks fero, i was able to see and then fix the error by using error_reporting(E_ALL); ini_set('display_errors', 'On'); above the rogue line. thanks for your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.