Welcome to the Webmaster Forum.
Results 1 to 5 of 5
  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    36

    differences between require and include

    What are the differences between require and include, include_once and require_once?

  2. #2
    Senior Member
    Join Date
    Mar 2013
    Posts
    118

    Re: differences between require and include

    Require will always read/execute the file, and include wont. You can use a include in if statements and loops where it'll only execute if the statement is true, and you cannot do that with require.

  3. #3
    Senior Member
    Join Date
    Aug 2013
    Posts
    200

    Re: differences between require and include

    In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.

    The include and require statements are used to insert useful codes written in other files, in the flow of execution.

    Include and require are identical, except upon failure:

    require will produce a fatal error (E_COMPILE_ERROR) and stop the script
    include will only produce a warning (E_WARNING) and the script will continue

    So, 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.
    Syntax
    include 'filename';

    or

    require 'filename';

    PHP include and require Statement
    Basic Example

    Assume that you have a standard header file, called "header.php". To include the header file in a page, use include/require:
    <html>
    <body>

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

    </body>
    </html>
    Example 2

    Assume we have a standard menu file that should be used on all pages.

    "menu.php":
    echo '<a href="/default.php">Home</a>
    <a href="/tutorials.php">Tutorials</a>
    <a href="/references.php">References</a>
    <a href="/examples.php">Examples</a>
    <a href="/about.php">About Us</a>
    <a href="/contact.php">Contact Us</a>';

    All pages in the Web site should include this menu file. Here is how it can be done:
    <html>
    <body>

    <div class="leftmenu">
    <?php include 'menu.php'; ?>
    </div>

    <h1>Welcome to my home page.</h1>
    <p>Some text.</p>

    </body>
    </html>
    Example 3

    Assume we have an include file with some variables defined ("vars.php"):
    <?php
    $color='red';
    $car='BMW';
    ?>

    Then the variables can be used in the calling file:
    <html>
    <body>

    <h1>Welcome to my home page.</h1>
    <?php include 'vars.php';
    echo "I have a $color $car"; // I have a red BMW
    ?>

    </body>
    </html>

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    12

    Re: differences between require and include

    It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

    The include and require statements are identical, except upon failure:

    require will produce a fatal error (E_COMPILE_ERROR) and stop the script
    include will only produce a warning (E_WARNING) and the script will continue
    So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement 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.

  5. #5
    Senior Member
    Join Date
    Jul 2015
    Location
    Delhi
    Posts
    249

    Re: differences between require and include

    include( ) will attempt to load the specified file, but will allow the script to continue if not successfully loaded.

    require( ), on the other hand will cause a "Fatal Error" to occur if the specified file is not successfully loaded.

 

 

Similar Threads

  1. Replies: 4
    Last Post: 08-08-2013, 02:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Back to top

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203