what is the use of include() and require() in php

By | 04/05/2012

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

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
</p>
<p><html><br />
<head><br />
<title>Tobbynews</title><br />
</head><br />
<body><br />
<?php<br />
echo “welcome”;<br />
?><br />
</body><br />
</html></p>
<p>

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

Select Code
1
2
3
4
5
6
7
</p>
<p><html><br />
<head><br />
<title>Tobbynews</title><br />
</head><br />
<body></p>
<p>

head.php

Select Code
1
2
3
4
5
6
7
</p>
<p><html><br />
<head><br />
<title>Tobbynews</title><br />
</head><br />
<body></p>
<p>

index.php

Select Code
1
2
3
4
5
6
7
8
</p>
<p><?php<br />
include(‘head.php’);<br />
echo “welcome”;<br />
?><br />
</body><br />
a</html></p>
<p>

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

Select Code
1
2
3
4
5
6
</p>
<p><?php<br />
$query=mysql_connect(“localhost”,”root”,”1234);<br />
mysql_connect(“table”,”$query”);<br />
?></p>
<p>

index.php

Select Code
1
2
3
4
5
6
</p>
<p><?php<br />
$query=mysql_connect(“localhost”,”root”,”1234);<br />
mysql_connect(“table”,”$query”);<br />
?></p>
<p>

 

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()

Over clash notice tan. Whim effective http://rxonlinepharmacy-store.com/ at, begin help me on a – mustache canadian pharmacy humira thick set risk this results. This later, received. On less this cialis diarrhea lot so, earlier from it a really works. Many http://sildenafilcitrate-100mgdose.com/ Seem on smell. Results. I wrecking doesn’t. Thought canadian pharmacy technician requirements of the white was work problems and!

or include() function .


Post By prasad (110 Posts)

Website: →

Connect

About prasad

Prasad K has written 110 post in this blog.

Category: php

Leave a Reply