FPublisher
Web-технологии: База знаний
Документация PHP
include()
The include() statement includes and evaluates the specified file.
The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in included file doesn’t cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/ , you included include/a.php and there is include «b.php» in that file, b.php is first looked in /www/libraries/ and then in /www/include/ . If filename begins with ./ or ../, it is looked only in the current working directory.
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
Пример #1 Basic include() example
vars.php
= ‘green’ ;
$fruit = ‘apple’ ;
?>
test.php
echo «A $color $fruit» ; // A
echo «A $color $fruit» ; // A green apple
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
Пример #2 Including within functions
function foo ()
<
global $color ;
echo «A $color $fruit» ;
>
/* vars.php is in the scope of foo() so *
* $fruit is NOT available outside of this *
* scope. $color is because we declared it *
* as global. */
foo (); // A green apple
echo «A $color $fruit» ; // A green
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
If «URL fopen wrappers» are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper — see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file’s variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Версии PHP для Windows до PHP 4.3.0 не поддерживают возможность использования удаленных файлов этой функцией даже в том случае, если опция allow_url_fopen включена.
Пример #3 include() through HTTP
/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, ‘Works’ here means that the variables
* $foo and $bar are available within the included file. */
// Won’t work; file.txt wasn’t handled by www.example.com as PHP
include ‘http://www.example.com/file.txt?foo=1&bar=2’ ;
// Works.
include ‘http://www.example.com/file.php?foo=1&bar=2’ ;
$foo = 1 ;
$bar = 2 ;
include ‘file.txt’ ; // Works.
include ‘file.php’ ; // Works.
Security warning
Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, readfile() is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code.
See also Remote files, fopen() and file() for related information.
Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it’s possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.
Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.
Пример #4 Comparing return value of include
// won’t work, evaluated as include((‘vars.php’) == ‘OK’), i.e. include(»)
if (include( ‘vars.php’ ) == ‘OK’ ) <
echo ‘OK’ ;
>
// works
if ((include ‘vars.php’ ) == ‘OK’ ) <
echo ‘OK’ ;
>
?>
Пример #5 include() and the return() statement
?>
testreturns.php
= include ‘return.php’ ;
echo $foo ; // prints ‘PHP’
$bar = include ‘noreturn.php’ ;
echo $bar ; // prints 1
$bar is the value 1 because the include was successful. Notice the difference between the above examples. The first uses return() within the included file while the other does not. If the file can’t be included, FALSE is returned and E_WARNING is issued.
If there are functions defined in the included file, they can be used in the main file independent if they are before return() or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn’t complain about functions defined after return(). It is recommended to use include_once() instead of checking if the file was already included and conditionally return inside the included file.
Another way to «include» a PHP file into a variable is to capture the output by using the Output Control Functions with include(). For example:
Пример #6 Using output buffering to include a PHP file into a string
function get_include_contents ( $filename ) <
if ( is_file ( $filename )) <
ob_start ();
include $filename ;
$contents = ob_get_contents ();
ob_end_clean ();
return $contents ;
>
return false ;
>
In order to automatically include files within scripts, see also the auto_prepend_file and auto_append_file configuration options in php.ini .
Замечание: Поскольку это языковая конструкция, а не функция, она не может вызываться при помощи переменных функций
Warned that parse error
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is . , current working directory is /www/ , you included include/a.php and there is include «b.php» in that file, b.php is first looked in /www/ and then in /www/include/ . If filename begins with ./ or ../ , it is looked only in include_path relative to the current working directory.
?>
test.php
echo «A $color $fruit» ; // A
echo «A $color $fruit» ; // A green apple
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.
Пример 16-6. Including within functions
echo «A $color $fruit» ;
>
/* vars.php is in the scope of foo() so *
* $fruit is NOT available outside of this *
* scope. $color is because we declared it *
* as global. */
foo (); // A green apple
echo «A $color $fruit» ; // A green
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags .
Внимание | ||
/* This example assumes that www.example.com is configured to parse .php * files and not .txt files. Also, ‘Works’ here means that the variables * $foo and $bar are available within the included file. */ // Won’t work; file.txt wasn’t handled by www.example.com as PHP // Works. $foo = 1 ;
?> testreturns.php echo $foo ; // prints ‘PHP’ $bar = include ‘noreturn.php’ ; echo $bar ; // prints 1 $bar is the value 1 because the include was successful. Notice the difference between the above examples. The first uses return() within the included file while the other does not. If the file can’t be included, FALSE is returned and E_WARNING is issued. Another way to «include» a PHP file into a variable is to capture the output by using the Output Control Functions with include() . For example: Пример 16-11. Using output buffering to include a PHP file into a string function get_include_contents ( $filename ) < In order to automatically include files within scripts, see also the auto_prepend_file and auto_append_file configuration options in php.ini .
Если Вы не нашли что искали, то рекомендую воспользоваться поиском по сайту: 4 Different Types of Errors in PHPHome » DevOps and Development » 4 Different Types of Errors in PHP A PHP Error occurs when something is wrong in the PHP code. The error can be as simple as a missing semicolon, or as complex as calling an incorrect variable. To efficiently resolve a PHP issue in a script, you must understand what kind of problem is occurring. The four types of PHP errors are: Tip: You can test your PHP scripts online. We used an online service to test the code mentioned in this article. Warning ErrorA warning error in PHP does not stop the script from running. It only warns you that there is a problem, one that is likely to cause bigger issues in the future. The most common causes of warning errors are:
As there is no “external_file”, the output displays a message, notifying it failed to include it. Still, it doesn’t stop executing the script. Notice ErrorNotice errors are minor errors. They are similar to warning errors, as they also don’t stop code execution. Often, the system is uncertain whether it’s an actual error or regular code. Notice errors usually occur if the script needs access to an undefined variable. In the script above, we defined a variable ($a), but called on an undefined variable ($b). PHP executes the script but with a notice error message telling you the variable is not defined. Parse Error (Syntax)Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the script. Parse errors are caused by:
For example, the following script would stop execution and signal a parse error: It is unable to execute because of the missing semicolon in the third line. Fatal ErrorFatal errors are ones that crash your program and are classified as critical errors. An undefined function or class in the script is the main reason for this type of error. There are three (3) types of fatal errors:
For instance, the following script would result in a fatal error: The output tells you why it is unable to compile, as in the image below: Distinguishing between the four types of PHP errors can help you quickly identify and solve problems in your script. Make sure to pay attention to output messages, as they often report on additional issues or warnings. If you are trying to locate a bug on your website, it is also important to know which PHP version your web server is running. Adblockdetector |