PHP/Utility Function/include
Содержание
Behavior of Files Included Using include
<source lang="html4strict">
//File: test.inc <?php
echo "Inside the included file
"; return "Returned String"; echo "After the return inside the include
";
?> <?php
echo "Inside of includetest.php
"; $ret = include ("test.inc"); echo "Done including test.inc
"; echo "Value returned was "$ret"";
?>
</source>
Including files relative to the current file
<source lang="html4strict">
<?php $currentDir = dirname(__FILE__); include $currentDir . "/functions.php"; include $currentDir . "/classes.php"; ?>
</source>
Including Other Files
<source lang="html4strict">
//foo.php:
<?php print "Starting foo\n"; include "bar.php"; print "Finishing foo\n"; ?>
//bar.php:
<?php print "In bar\n"; ?>
After including foo.php would look like this:
<?php print "Starting foo\n"; print "In bar\n"; print "Finishing foo\n"; ?> </source>
Using include() to Execute PHP and Assign the Return Value
<source lang="html4strict">
<html> <head> <title>Acquiring a Return Value with include()</title> </head> <body>
<?php $addResult = include("another.php"); print "The include file returned $addResult"; ?>
</body> </html>
//An Include File That Returns a Value //another.php
<?php $retval = ( 4 + 4 ); return $retval; ?> </source>
Using include to Load Files in PHP
<source lang="html4strict">
//File: library.inc <?
function is_leapyear($year = 2004) { $is_leap = (!($year % 4) && (($year % 100) || !($year % 400))); return $is_leap; }
?> <?php
include ("library.inc"); // Parentheses are optional $leap = is_leapyear(2003);
?>
</source>
Using include() Within a Loop
<source lang="html4strict">
<html> <head> <title>Using include() Within a Loop</title> </head> <body>
<?php for ( $x=1; $x<=3; $x++ ) {
$incfile = "incfile".$x.".txt";print "
";
print "Attempting include $incfile
";
include( "$incfile" );
print "
} ?>
</body> </html>
</source>