require_once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

require_once

Post by Lphp »

if I want to use require_once to including an other php file should I including a simple php file or a php web page ( have html, body......)
thank you :?
User avatar
getmizanur
Forum Commoner
Posts: 71
Joined: Sun Sep 06, 2009 12:28 pm

Re: require_once

Post by getmizanur »

you can either have php or html files in a required statement does not matter. for good programming practice you should use required_once and include_once to call php files
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: require_once

Post by social_experiment »

Lphp wrote:a simple php file or a php web page
Either will work and remember to suppress (when the site is live) any error's that might arise should the file you want to include isn't found for whatever reason.

Code: Select all

<?php
 // '@' stops a warning message from being displayed
 @include_once 'footer.php';
?>
A note about the two functions: if you use include_once() the script will continue to execute even the file you want to include cannot be found. With require_once a fatal error is issued and the execution stops.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

social_experiment wrote: Either will work and remember to suppress (when the site is live) any error's that might arise should the file you want to include isn't found for whatever reason.
Is that not dangerous????? If you are including or requiring a file, would you not want it to succeed always or give an error in any environment?

S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: require_once

Post by social_experiment »

eazyGen wrote:Is that not dangerous????? If you are including or requiring a file, would you not want it to succeed always or give an error in any environment?
Define how it's dangerous for me to suppress an error that will reveal information about my file system? Sure, i want this error when i am developing the script or when i am testing it on a localhost but not when it is live. Ever seen a site that looks really pretty and at the top you have this ugly, very revealing message telling you that page.php could not be found in directory x? Looks really unprofessional. The '@' operator isn't for dealing with errors, it's for stopping them from being displayed, error handling is a seperate, necessary concern.

Below, the type of error you are likely to recieve if a file cannot be included / or required
Fatal error: require_once() [function.require]: Failed opening required 'file2.php' (include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\pagex.php on line 2
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

social_experiment wrote:
eazyGen wrote:Is that not dangerous????? If you are including or requiring a file, would you not want it to succeed always or give an error in any environment?
Define how it's dangerous for me to suppress an error that will reveal information about my file system? Sure, i want this error when i am developing the script or when i am testing it on a localhost but not when it is live. Ever seen a site that looks really pretty and at the top you have this ugly, very revealing message telling you that page.php could not be found in directory x? Looks really unprofessional. The '@' operator isn't for dealing with errors, it's for stopping them from being displayed, error handling is a seperate, necessary concern.

Below, the type of error you are likely to recieve if a file cannot be included / or required
Fatal error: require_once() [function.require]: Failed opening required 'file2.php' (include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\pagex.php on line 2
Sorry, but I could hardly disagree more.

Sure you want as much detail in your error messages when you are developing. They help you fix and perfect your code.

However, if you suppress a fatal error in a live system environment, you are letting your code run riot and any kind of disaster could follow (including data corruption) and nobody would know - at least not until other parts of the application start to crash for who knows what reason.

Moreover, this particular type of error should always be caught by the software and then dealt with accordingly (an orderly shutdown with message) because the cause of the error can be beyond the programmer's control - that is to say, an operations department, separate from the development department, may break the directory structure of the app. I have seen this happen at first hand.

In summary, if your app REQUIRES something, and if it doesn't get it, you want to make as much noise as possible as soon as possible. Anything else is very poor practice. Never mind if a fatal error doesn't look professional - data corruption looks far worse. And, lastly, error messages and error handling are all part of the same exercise. They share the same objectives and should not be dissociated.

S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: require_once

Post by social_experiment »

The error control operator only suppresses the error so it's logical that the above disaster / data corruption could still occur, regardless of whether the error is displayed or not. If you have the same error reporting mechanisms for a unsuppressed error, why not have it for a suppressed error?

Looking at the manual, and at the different opinions here, i would suggest the OP uses include_once() as opposed to require_once().
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

peeer wrote:Your question is beyond the scope of my knowledge I am sorry for I can't give you the answer that your want. but I believe others will give you what you want Hope next time i can do your favor
[url=http://www.mytoryburchsales.com/]tory burch sale
[url=http://www.poloshirtsonsales.com/mens-ralph-lauren-stripe-polo-c-7.html]Men's Ralph Lauren Stripe Polo
[url=http://www.slimmingshops.com/]slimming capsule
[url=http://www.poloshirtsonsales.com/]Cheap Polo Shirt
Errrr - this looks a bit spammy :banghead:

S
Last edited by eazyGen on Tue Aug 30, 2011 11:38 pm, edited 2 times in total.
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

social_experiment wrote:The error control operator only suppresses the error so it's logical that the above disaster / data corruption could still occur, regardless of whether the error is displayed or not. Yes indeed - and that is why it is bad practice.

If you have the same error reporting mechanisms for a unsuppressed error, why not have it for a suppressed error? Why suppress errors at all?

Looking at the manual, and at the different opinions here, i would suggest the OP uses include_once() as opposed to require_once().

I think that is what the OP wishes to use. To answer his question - you can include anything you like really, but I would advise that you spend a little time considering your application structure first to enable you to make use of includes (once or otherwise - or requires for that matter) to best effect.
Thinking on a little further, a couple more questions occur to me:

1. Why use include and not require? If you want to include something, does that not mean you require it and want to know therefore, if you cannot obtain it?
2. Why use require or include once? If you have structured your application correctly, then you probably have everything included / required in one place, and everything works. If you include / require once, should you not say to yourself - "I may be including something more than once and so I should look at my structure rather than suppressing the error/message that gets flagged when I hit a duplicate include / require?

S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: require_once

Post by social_experiment »

eazyGen wrote:Yes indeed - and that is why it is bad practice.
My point: With or without the @, the error will cause damage, i prefer to handle it quietly. Imo that's not bad, nor is your opinion, it's two different methods of handling a similar problem.
eazyGen wrote:Why suppress errors at all?
Letting the application die gracefully?
eazyGen wrote:1. Why use include and not require? If you want to include something, does that not mean you require it and want to know therefore, if you cannot obtain it?
Seeing as the two constructs are identical in every way except how they handle failure, why not use include? Include / include_once makes it slightly easier to report back on any non-included file. With require / require_once, the fatal error stops script execution after the statement is parsed and the required file cannot be found.
eazyGen wrote:2. Why use require or include once? If you have structured your application correctly, then you probably have everything included / required in one place, and everything works. If you include / require once, should you not say to yourself - "I may be including something more than once and so I should look at my structure rather than suppressing the error/message that gets flagged when I hit a duplicate include / require?
I would think that this is a performance related question, once code in the file is already included, why do it everytime the page is loaded.
The Manual wrote:include_once() may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

I am afraid I don't really know what you mean by :

"prefer to handle it quietly"

and

"Letting the application die gracefully?"

I am not sure we will reach agreement so perhaps we should just leave it there.

S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: require_once

Post by social_experiment »

Letting an application die gracefully is (to my understanding) to let anything that causes an error (fatal or otherwise) not reveal any information about the server, script, database, etc to the user who is using the application at the time.

Code: Select all

<?php
 // this
 @mysql_connect($host, $user, $pass) or die('Cannot connect to database');
 // as opposed to, which will reveal the username should
 // the connection not be made.
 mysql_connect($host, $user, $pass)
eazyGen wrote:I am not sure we will reach agreement so perhaps we should just leave it there.
Ditto, i only wish to share my opinion
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

On a slightly different tack (sorry if this represent an off topic point).

I am working my way through a bunch of tutorials and work books. On one (PHP-MySQL_Essential_Training from Lynda.com) I got to the stage where the tutor was creating a basic CMS system and started to include files.

He included a header and a footer, which seems fine. Except to say that in the included header file there were divs opened but not closed. He relied on the included footer to close them.

That can't be right surely??????? That would seem to me to be absurdly fragile and liable to fall apart without it being very clear why.

I freely admit I am new to PHP, but I would have thought that included files should at least be self contained?

A couple of other things.

1. For every line of PHP he opens and then closes the PHP tag. Surely he is giving the web server an unnecessarily hard time by doing this?
2. He also justified the use of includes rather than requires by saying, and I quote: "It's just HTML, so there's no real damage if it's not there". I'm sorry but that's baloney in my view.

S

Any thoughts,

S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: require_once

Post by social_experiment »

eazyGen wrote:He included a header and a footer, which seems fine. Except to say that in the included header file there were divs opened but not closed. He relied on the included footer to close them.
Wordpress employs a similar method of creating headers / footers, i also am dumbstruck by why because it would be really easy just to close a tag in one file as opposed to closing it in another one.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Re: require_once

Post by eazyGen »

social_experiment wrote:
eazyGen wrote:He included a header and a footer, which seems fine. Except to say that in the included header file there were divs opened but not closed. He relied on the included footer to close them.
Wordpress employs a similar method of creating headers / footers, i also am dumbstruck by why because it would be really easy just to close a tag in one file as opposed to closing it in another one.
My thoughts exactly 8O

At this stage I can't ever see myself adopting such an approach. The whole concept of OO shows us the benefit of re-using self-contained code.

Wow.

S
Post Reply