include와 require 사용의 차이점에 대한 분석 예

怪我咯
풀어 주다: 2023-03-12 20:28:01
원래의
996명이 탐색했습니다.

PHP에서 includerequire의 차이점에 대한 정보가 인터넷에 너무 많습니다. 그런데 이게 정말 사실일까요? 오늘은 구체적인 예시를 통해 간략하게 분석하고 검증해보겠습니다

먼저 command.php 파일을 편집하고

echo 'hello'.PHP_EOL;
로그인 후 복사

그 다음 console.php 파일을 편집하고

for($i=1;$i<=3;++$i){
	require &#39;command1.php&#39;;
}
로그인 후 복사

원래는 이 echo를 포함해서 실행하려고 했는데요. , 하지만 잘못된 파일 이름을 쓸 생각은 하지 않았습니다. require인 경우 다음과 같은 오류가 보고됩니다:

Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4

Fatal error: require(): Failed opening required &#39;command1.php&#39; (include_path=&#39;.&#39;) in console.php on line 4
PHP Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4
PHP Fatal error: require(): Failed opening required &#39;command1.php&#39; (include_path=&#39;.&#39;) in console.php on line 4
로그인 후 복사

require를 include

for($i=1;$i<=3;++$i){
	include &#39;command1.php&#39;;
}
로그인 후 복사

로 변경하면 다음과 같은 오류가 보고됩니다:

Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4

Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4

Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4

Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4

Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4

Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4
PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4
PHP Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4
PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4
PHP Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4
PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4
PHP Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4
로그인 후 복사

require_once 또는 include_once를 사용하는 경우 포함 경로가 올바른 한 loop은 한 번만 실행됩니다.

요약:

require를 사용하세요. 파일이 성공적으로 포함되지 않으면 치명적인 오류가 보고되고 전체 프로그램이 종료됩니다.

Include를 사용하세요. 파일이 성공적으로 포함되지 않으면 일반 경고가 보고되고 후속 코드는 계속 실행됩니다.

웹 프로그램이 파일에 대한 의존성이 강한 디자인 방법인 MVC를 사용하는 경우 require_once를 사용하세요.

위 내용은 include와 require 사용의 차이점에 대한 분석 예의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!