require_once
Posted: Sun Aug 28, 2011 9:32 pm
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
thank you
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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.Lphp wrote:a simple php file or a php web page
Code: Select all
<?php
// '@' stops a warning message from being displayed
@include_once 'footer.php';
?>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?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.
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.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?
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.social_experiment wrote: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.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?
Below, the type of error you are likely to recieve if a file cannot be included / or requiredFatal 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
Errrr - this looks a bit spammypeeer 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
Thinking on a little further, a couple more questions occur to me: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.
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:Yes indeed - and that is why it is bad practice.
Letting the application die gracefully?eazyGen wrote:Why suppress errors at all?
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: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?
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.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?
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.
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)
Ditto, i only wish to share my opinioneazyGen wrote:I am not sure we will reach agreement so perhaps we should just leave it there.
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.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.
My thoughts exactlysocial_experiment wrote: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.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.