Php include and require

we are using include and require for add or call other php classes and function in to another file .Something like extending.Therefore we can reuse the previous codes to our new file or function.
Say in java we are using import to get a jar file for extensions similarly we are using include in php.

Include –> This will produce only warnings if we have an error and script will continue (E_COMPILE_ERROR)

<html>
<body>
<?php include 'Needed.php'; ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>

 

Require –> This will produce fatal error then it will stop the script (E_WARNING)

<html>
<body>
<?php require 'Needed.php'; ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>

 



If you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of Framework, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

No comments:

Post a Comment