Jump to content

Recommended Posts

Posted

hi,

 

i'm just wondering what the difference is between 'require' and 'include' in php.

 

just to illustrate my point, 'a.php' includes the following lines

<?php

function a() {

......

}

?>\n\n\n (which get added every time i edit a file in cpanel for some reason)

 

and in 'b.php', if i use

 

require 'a.php'

header("Content-type.........

 

it would fail because the "\n\n" will be sent before the header, and of course header information can't be sent after there's already been output.

 

but the problem goes away if i use 'include' instead of 'require'.

 

why is that????

Posted (edited)

Taken directly from PHP.net:

The two constructs are identical in every way except how they handle failure. include() produces a Warning while require()  results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless.

 

So basically, require is just a little more strict, and better to use if the included file is required for the rest of the script/page to function properly.

 

Not sure about the header part, maybe someone who codes in it more regularly can chime in.

Edited by TCH-MikeJ
Posted

If you are going to use the header function

you must not output anything even a space before calling it

and when you include or require, you have to check each file for any output

look for blank lines with a space.

its best to put anything you need to output from an included file into a string and then output it after the header function.

 

Cpanels file manager is not the best for editing php.

You should get a good text editor.

  • 2 weeks later...
Posted
Cpanels file manager is not the best for editing php.

You should get a good text editor.

 

yeah think i've got that one sorted out... thanks guys.....

and yeah the only reason i edit it in cpanel is because it's probably easier....

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...