PHP/Utility Function/include — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 10:37, 26 мая 2010

Behavior of Files Included Using include

 
//File: test.inc
<?php
    echo "Inside the included file<BR>";
    return "Returned String";
    echo "After the return inside the include<BR>";
?>
<?php
    echo "Inside of includetest.php<BR>";
    $ret = include ("test.inc");
    echo "Done including test.inc<BR>";
    echo "Value returned was "$ret"";
?>



Including files relative to the current file

 
<?php
$currentDir = dirname(__FILE__);
include $currentDir . "/functions.php";
include $currentDir . "/classes.php";
?>



Including Other Files

 
//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";
    ?>



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

 
<html>
<head>
<title>Acquiring a Return Value with include()</title>
</head>
<body>
<div>
<?php
$addResult = include("another.php");
print "The include file returned $addResult";
?>
</div>
</body>
</html>

//An Include File That Returns a Value
//another.php
 <?php
 $retval = ( 4 + 4 );
 return $retval;
 ?>



Using include to Load Files in PHP

 
//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);
?>



Using include() Within a Loop

 
<html>
<head>
<title>Using include() Within a Loop</title>
</head>
<body>
<div>
<?php
for ( $x=1; $x<=3; $x++ ) {
 $incfile = "incfile".$x.".txt";
 print "<p>";
 print "Attempting include $incfile<br/>";
 include( "$incfile" );
 print "</p>";
}
?>
</div>
</body>
</html>