php_mysql_apache [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

php_mysql_apache [Electronic resources] - نسخه متنی

Julie C. Meloni

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید









Including Files with include()



The include() statement enables you to incorporate files into your PHP documents. PHP code in these files can be executed as if it were part of the main document. This can be useful for including library code in multiple pages.


Having created a really useful function, your only option until now would have been to paste it into every document that needs to use it. Of course, if you discover a bug or want to add a feature, you would have to find every page that uses the function to make the change. The include() statement can save you from this chore. You can add the function to a single document and, at runtime, read it into any page that needs it. The include() statement requires a single argument: a relative path to the file to be included. Listing 11.1 creates a simple PHP script that uses include() to incorporate and output the contents of a file.


Listing 11.1 Using include()


1: <html>
2: <head>
3: <title>Listing 11.1 Using include()</title>
4: </head>
5: <body>
6: <?php
7: include("listing11.2.php");
8: ?>
9: </body>
10: </html>


The include() statement in Listing 11.1 incorporates the document listing11.2.php, the contents of which you can see in Listing 11.2.


Listing 11.2 The File Included in Listing 11.1


1: I have been included!!


Put the contents of Listing 11.1 in a file named listing11.1.php, and the contents of Listing 11.2 in a file named listing11.2.php. Place both files in your Web server document root. When you access listing11.1.php through your Web browser, the output on the screen is




I have been included!!


This might seem strange to you, given that we''ve included plain text within a block of PHP code. In fact, the contents of an included file are displayed as text by default. If you want to execute PHP code in an included file, you must enclose it in PHP start and end tags. In Listings 11.3 and 11.4, we amend the previous example so that code is executed in the included file.


Listing 11.3 Using the include() Statement to Execute PHP in Another File


1: <html>
2: <head>
3: <title>Listing 11.3 Using include to execute PHP in another file</title>
4: </head>
5: <body>
6: <?php
7: include("listing11.4.php");
8: ?>
9: </body>
10: </html>

Listing 11.4 An Include File Containing PHP Code


1: <?php
2: echo "I have been included!!<BR>";
3: echo "But now I can add up... 4 + 4 = ".(4 + 4);
4: ?>


Put the contents of Listing 11.3 in a file named listing11.3.php, and the contents of Listing 11.4 in a file named listing11.4.php. Place both these files in your Web server document root. When you access listing11.3.php through your Web browser, the output on the screen is




I have been included!!
But now I can add up... 4 + 4 = 8

Returning a Value from an Included Document



Included files in PHP can return a value in the same way that functions do. As in a function, using the return statement ends the execution of code within the included file. Additionally, no further HTML is included. In Listings 11.5 and 11.6, we include a file and assign its return value to a variable.


Listing 11.5 Using include() to Execute PHP and Assign the Return Value


1: <html>
2: <head>
3: <title>Listing 11.5 Using include() to execute PHP and
4: assign the return value</title>
5: </head>
6: <body>
7: <?php
8: $addResult = include("listing11.6.php");
9: echo "The include file returned $addResult";
10: ?>
11: </body>
12: </html>

Listing 11.6 An Include File That Returns a Value


1: <?php
2: $retval = ( 4 + 4 );
3: return $retval;
4: ?>
5: This HTML will never be displayed because it comes after a return statement!


Put the contents of Listing 11.5 in a file named listing11.5.php, and the contents of Listing 11.6 in a file named listing11.6.php. Place both of these files in your Web server document root. When you access listing11.5.php through your Web browser, the output is




The include file returned 8

Using include() Within Control Structures



You can use an include() statement in a conditional statement, and the referenced file is read only if the condition is met. For example, the include() statement in the following fragment will never be called:




$test = false;
if ($test) {
include("a_file.txt"); // won''t be included
}


If you use an include() statement within a loop, it''s replaced with the contents of the referenced file each time the include() statement is called. This content is executed for every call. Listing 11.7 illustrates this concept by using an include() statement in a for loop. The include() statement references a different file for each iteration.


Listing 11.7 Using include() Within a Loop


1: <html>
2: <head>
3: <title>Listing 11.7 Using include() within a loop</title>
4: </head>
5: <body>
6: <?php
7: for ($x = 1; $x<=3; $x++) {
8: $incfile = "incfile$x".".txt";
9: echo "Attempting include $incfile<br>";
10: include("$incfile");
11: echo "<br>";
12: }
13: ?>
14: </body>
15: </html>


When Listing 11.7 is run, it includes the content of three different files: incfile1.txt, incfile2.txt, and incfile3.txt. Assuming that each of these files simply contains a confirmation of its own name, the output should look like Figure 11.1.


Figure 11.1. Output of Listing 11.7.




include_once()



One of the problems caused by using multiple libraries within your code is the danger of calling include() twice on the same file. This can occur in larger projects when different library files call include() on a common file. Including the same file twice often results in repeated declarations of functions and classes, thereby causing the PHP engine great unhappiness.


The situation is saved by the include_once() statement. include_once() requires the path to an include file and behaves the same way as include() the first time it''s called. However, if include_once() is called again for the same file during script execution, the file is


not included again. This makes include_once() an excellent tool for the creation of reusable code libraries!


The include_path Directive



Using include() and include_once() to access libraries can increase the flexibility and reusability of your projects. However, there are still headaches to overcome. Portability in particular can suffer if you hard-code the paths to included files. Imagine that you create a lib directory and reference it throughout your project:




include_once("/home/user/bob/htdocs/project4/lib/mylib.inc.php");


When you move your project to a new server, you might find that you have to change a hundred or more include paths. You can escape this fate by setting the include_path directive in your php.ini file:




include_path .:/home/user/bob/htdocs/project4/lib/


The include_path can include as many directories as you want, separated by colons (semicolons in Windows). The order of the items in the include_path directive determines the order in which the directories are searched for the named file. The first dot (.) before the first colon indicates "current directory." You can then reference your library file by only its name:




include_once("mylib.inc.php");


When you move your project, you need to change only the include_path directive.



PHP has both a require() statement, which performs a similar function to include(), and a require_once() statement.


require() is executed regardless of a script''s flow, and therefore shouldn''t be used as part of conditional or loop structures.


A file included as a result of a require() statement cannot return a value.




/ 323