I have PHP file where I have defined the server access variables as well as the
mysql_select_db, as this functions are regularly used in almost every page in backend, while I am using
include() which is perfectly working for me now, which method or function would you suggest and I would like to know if there is any flaw if I use
include() or is it safe to use it?
Edit : Keeping in mind I’ll be using
$_SESSION variable too.
The only difference between the two is that
require and its sister
require_once throw a fatal error if the file is not found, whereas
include_once only show a warning and continue to load the rest of the page. If you don’t want PHP to attempt to load the rest of your page without the database info (which I would assume), then use
require_once. You don’t need to include the file more than once, so there is no need to use the regular
Functional Work :
All functions perform similar work. All functions will include and evaluates the specific file while executing the code.
Functional Difference :
include vs include_once : There is only one difference between include() and include_once(). If the code from a file has been already included then it will not be included again if we use include_once(). Means include_once() include the file only once at a time.
include vs require : if include() is not able to find a specified file on location at that time it will throw a warning however, it will not stop script execution. For the same scenario, require() will throw a fatal error and it will stop the script execution.
require vs require_once : There is only one difference between require() and require_once(). If the code from a file has been already included then it will not be included again if we use require_once(). Means require_once() include the file only once at a time.
To get the detailed knowledge with example please review these amazing articles
For the database connection variables, use of require_once() function will be preferable.
If the connection fails for any reason you can show the failure message.
If your page will not work without the DB connection, then require_once would be the only correct option (since you don’t want to load these settings twice, loading them once should suffice). Include will try to load your page even if the settings file is not available.
The include() statement includes and evaluates the specified file.
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.
require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page.
The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.
<?php include('db.php'); echo "<br>"."Included"."<br>"; include_once('db.php'); echo "<br>"."Again included"."<br>"; ?>
In the Above Code, I have included a file using include statement at the top,
the file get included.
Next I have used include_once to include the same file, But as the file was already included
above, it will not be included again here.
Connected -----This is from db.php File Included Again included
include_once('db.php'); echo "<br>"."Again included"."<br>"; include('db.php'); echo "<br>"."Included"."<br>"; ?>
In the above code, I have used include_once at the top, so the file is included
But in the next code I have again used include_once for the same file, then
again the file will get included and the output will be
Again included Connected Included Connected
require() is better for you. Because with require file includes before script compilation. inluce() using in dinamical including.
You should use include_once() if you’re including it more than once on a page.