0

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>
Community
  • 1
  • 1

3 Answers3

3

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
?>
Damien
  • 5,872
  • 2
  • 29
  • 35
  • Thanks. I did it and I see that require gives me an error on time while include gives me the error 10 times. The difference between them is getting more confused now.include should not give error. –  Jan 14 '12 at 16:15
  • 1
    Include give you a warning, but don't stop the execution of your page. – Damien Jan 14 '12 at 16:22
0

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>

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

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.

DerVO
  • 3,679
  • 1
  • 23
  • 27