user379888
user379888

Reputation:

The error thrown by require not visible

I was reading the difference between include and require in php here.

require will throw a PHP Fatal Error if the file cannot be loaded. 

I created a test file in php to get more understanding about the difference but both of them do not show anything(I do not see any error in require).

Please help me out. Thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
for( $value = 0; $value < 10; $value++ )
if($value>10)
require("boom.php"); // no such file exits in real
?>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    for( $value = 0; $value < 10; $value++ )
    if($value>10)
    include("boom.php"); // no such file exits in real
    ?>
    </body>
    </html>

Upvotes: 0

Views: 118

Answers (3)

Damien
Damien

Reputation: 5882

Your testing code is wrong, $value will never be greater than 10. Try this and you will have your Fatal Error:

<?php
require("boom.php"); // no such file exits in real
?>

Upvotes: 3

DerVO
DerVO

Reputation: 3679

Maybe display_errors is disabled. You can check this by calling phpinfo().

Try to place

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

at the beginning of your script, to show errors directly on screen (only recommendable for development systems).

EDIT Doh! I go with Damien's answer.

Upvotes: 0

Marc B
Marc B

Reputation: 360912

Probably your PHP has display_errors turned off, which means you will not see error messages in client-side output. You should enable this setting in your php.ini on your development environments.

If you had something something like this, you'd at least have seen the failure:

<html>

<body>
<p>Before require</p>
<?php require('does-not-exist'); ?>
<p>After require</p>
</body>

</html>

With some actual output in there, you'd see that only the 'before require' text is output - the script will terminate execution when the require() fails.

With your version, you have no visible output and would have to look at the page source in your browser to see tha there's no </body></html>

Upvotes: 0

Related Questions