Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

Require vs Include in php

Mon Sep 08, 2008 2:01 am

Require command do the same things like what you do in include command ,but there is a great different between these commands and it is in case when the called page not found .

In include command . suppose the following code :
Code:
<?php
include("example.php");
echo "Am here !";
?>

if example.php not found you will have the following output :
Code:
Warning: main(example.php): failed to open stream: No such file or directory in /codemiles.com/Folder/test.php on line 2 Warning: main(): Failed opening 'example.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /codemiles.com/Folder/test.php on line 2

Am here !


As you can see "Am here !" is displayed .So echo statements is working even after the warring because the warning don't prevent php script from running after it happen.
In the case of require command :
Code:
<?php
require("example.php");
echo "Am here !";
?>


the output in case "example.php" not found :
Code:
Warning: main(example.php): failed to open stream: No such file or directory in /codemiles.com/Folder/test.php on line 2
Fatal error: main(): Failed opening required 'example.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /codemiles.com/Folder/test.php on line 2


As you see , the echo statement wasn't executed like in case of include .The reason that the "Fatal error " happened in this case stop echo statements from running .

People (PHP programmers ) recommends using require ,because it is meaningless to include a file which is missing or not found .

Thanks ,



Post a reply
  Related Posts  to : Require vs Include in php
 require immediate help regarding asp.net     -  
 PHP Include     -  
 Using include() Within a Loop     -  
 Using include function     -  
 get include path     -  
 jsp include directive not working     -