include();
require();
require_once();
All the above have one common function: they are used to import a code written in an external file. For example in php-mysql applications, a connection needs to be established with the database for carrying out operations in database. This is done via the mysql_connect() function which takes in the parameters of host, user and password. Now this function has to be called in all the files doing mysql tasks. So instead of that, this code is written in a connection.php file and then included in all the other files requiring database connection using the above three functions.
Coming to the difference part, require() as the name suggests is different in the sense that it produces a fatal error if the external file is not found, while include() just produces a warning and moves on with the code. The later may be undesirable at times, so require() is most often used. So in a nutshell, require() makes sure that the file is strictly imported.
require_once() is almost similar to require() except that it checks if the required file has already been imported, in the event of which it skips to the next code. This helps preventing more "redeclared" errors.
Hope this article was useful.
No comments:
Post a Comment