All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Difference Between Include And Require In PHP

Last Updated : Mar 11, 2024

Difference Between Include And Require In PHP

In this tutorial we will show you the solution of difference between include and require in PHP, both are used when one php file needs another php file support there we using ‘include’ and ‘require’.

Then we can execute some part of code with current file and when some part of program we need on more files usually we have to write each time.

So first we need to store that parts of program only on separate php file and using ‘include’ or ‘require’ we just define that php file name so we can use on any program.

Step By Step Guide On Difference Between Include And Require In PHP :-

The include or require statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML or text on multiple pages of a website.

The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included can’t be found.

In require() statement generates a fatal error and terminates the script.

<html>
    <body>
        <h1>Welcome All</h1>
        <p>Thank You</p>
        <?php require 'input.php';?>
        <?php include 'input.php';?>
    </body>
</html>
Input.php
<?php

echo "This line displayed from another php file";
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we just shows demo, as we know <h1> tag is a header tag used for prints header of line on webpage. Always header tag available from <h1> to <h6> those are size and thickness only vary.
  4. Within <h1> tag we just given one sentence and <p> used for describes paragraph in html webpage. Same as within <p> tag we defined one sentence only. Those <h1>,<p> tag sentence are optional we can use any html elements it’s our own choice.
  5. Within html elements we can embed php also by this we defined include and require statements within html code.
  6. Both statements refers input.php file It contain single line of text and displayed using echo() method.
  7. In our output <h1>, <p> tags texts displayed with input.php file contain line of sentence also. That input php file line of text displayed two times because we called same file within include() and require() statements so it printed on browser two times.

Conclusion :-

In conclusion we are able to know how to covert stdclass object to array using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it prints first <h1>, <p> tag contain texts and then another php file contain texts combined then displayed on webpage.

This concept mostly used on server database connection code because we can same connection concept on more number of files.

So we can write connection code on separate file and use it all times by just one line php code.

I hope this tutorial on difference between include and require in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪