php中的include和require的区别
主要关注红色标记语句即可。
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
PHP include and require Statements
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.
The include and require statements are identical, except upon failure:
-
require will produce a fatal error (E_COMPILE_ERROR) and stop the scriptinclude will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename';or
require 'filename';
PHP include Examples
Example 1
Assume we have a standard footer file called "footer.php", that looks like this:
echo "Copyright © 1999-" . date("Y") . " W3Schools.com
";?>
To include the footer file in a page, use the include statement:
Example
Welcome to my home page!
Some text.
Some more text.
Run example ?
Example 2
Assume we have a standard menu file called "menu.php":
echo 'Home -HTML Tutorial -
CSS Tutorial -
JavaScript Tutorial -
PHP Tutorial';
?>
All pages in the Web site should use this menu file. Here is how it can be done (we are using a
element so that the menu easily can be styled with CSS later):
Example
Welcome to my home page!
Some text.
Some more text.
Run example ?
Example 3
Assume we have a file called "vars.php", with some variables defined:
$color='red';$car='BMW';
?>
Then, if we include the "vars.php" file, the variables can be used in the calling file:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example ?
PHP include vs. require
The require statement is also used to include a file into the PHP code.
However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example ?
If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example ?
Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found. |

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

드림위버 CS6
시각적 웹 개발 도구

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는
