>백엔드 개발 >PHP 튜토리얼 >PHP에서 include와 require의 차이점을 비교하는 예

PHP에서 include와 require의 차이점을 비교하는 예

巴扎黑
巴扎黑원래의
2017-08-23 13:46:421629검색

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

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


echo 'hello'.PHP_EOL;

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


for($i=1;$i<=3;++$i){
	require &#39;command1.php&#39;;
}

원래 하고 싶었어요 include 그리고 이 에코를 실행하면 내가 잘못된 파일 이름을 썼다고는 예상하지 못했습니다. 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를 사용하는 경우 포함 경로가 올바른 한 루프는 한 번만 실행됩니다.

요약:

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

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

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

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.