what is the use of include() and require() in php
include() and require() are used to call the php files, this functions are used when a file is needed by more than one or two php files
For including file, code reuse
For example:
index.php
1 2 3 4 5 6 7 8 9 10 |
<html>
<head>
<title>Tobbynews</title>
</head>
<body>
<?php
echo “welcome”;
?>
</body>
</html>
|
This html codes are used in almost all php files, so we have to include this code in separate file and call that files where we wanted
1 2 3 4 5 |
<html>
<head>
<title>Tobbynews</title>
</head>
<body>
|
head.php
1 2 3 4 5 |
<html>
<head>
<title>Tobbynews</title>
</head>
<body>
|
index.php
1 2 3 4 5 6 |
<?php
include(‘head.php’);
echo “welcome”;
?>
</body>
a</html>
|
For including Database file
Database connection codes are needed in all pages. we can also manually give that database connection codes in all pages, but when username and password of the database get changed then we have to change in all php files. The best way avoid this is by, we can use include() and require() function.
db.php
1 2 3 4 |
<?php
$query=mysql_connect(“localhost”,”root”,”1234”);
mysql_connect(“table”,”$query”);
?>
|
index.php
1 2 3 4 |
<?php
$query=mysql_connect(“localhost”,”root”,”1234”);
mysql_connect(“table”,”$query”);
?>
|
If we want to change the database name or password for databse, we can change it in db.php file and this file can be call by using this require() or include() function .




