php 파일 에 포 함 된 몇 가지 방식 요약

4250 단어 php파일 포함
네 가지 문장
PHP 에 파일 을 불 러 오 는 네 개의 문구 가 있 습 니 다:include,require,includeonce、require_once。
기본 문법
require:require 함 수 는 보통 PHP 스 크 립 트 의 맨 앞 에 놓 여 있 습 니 다.PHP 가 실행 되 기 전에 require 가 지정 한 도입 파일 을 읽 고 도입 한 스 크 립 트 파일 을 포함 하고 실행 하려 고 합 니 다.require 의 작업 방식 은 PHP 의 실행 효율 을 향상 시 키 는 것 입 니 다.같은 웹 페이지 에서 한 번 설명 한 후에 두 번 째 는 설명 하지 않 습 니 다.그러나 마찬가지 로 도입 파일 을 중복 해석 하지 않 기 때문에 PHP 에서 순환 이나 조건 문 구 를 사용 하여 파일 을 도입 할 때 include 를 사용 해 야 합 니 다.
include:PHP 스 크 립 트 의 임의의 위치 에 놓 을 수 있 습 니 다.일반적으로 프로 세 스 제어 처리 부분 에 놓 습 니 다.PHP 스 크 립 트 가 include 가 지정 한 파일 에 실 행 될 때 만 포함 하고 실행 을 시도 합 니 다.이런 방식 은 프로그램 이 실 행 될 때의 절 차 를 단순화 할 수 있다.두 번 째 로 같은 파일 을 만 났 을 때 PHP 는 다시 한 번 설명 합 니 다.include 는 require 에 비해 실행 효율 이 많이 떨 어 지 는 동시에 파일 에 사용자 정의 함 수 를 포함 할 때 PHP 는 해석 과정 에서 함수 중복 정의 문제 가 발생 합 니 다.
require_once / include_once:각각 require/include 와 같은 역할 을 합 니 다.다른 것 은 그들 이 실행 할 때 목표 내용 이 이전에 가 져 왔 는 지 확인 하 는 것 입 니 다.가 져 왔 다 면 같은 내용 을 다시 도입 하지 않 습 니 다.
서로 구별 하 다
include 와 require:
include 는 반환 값 이 있 고 require 는 반환 값 이 없습니다.
include 파일 을 불 러 오 는 데 실 패 했 을 때 경고 가 생 성 됩 니 다(EWARNING)오류 발생 후 스 크 립 트 가 계속 실 행 됩 니 다.그래서 include 는 계속 실행 하고 사용자 에 게 결 과 를 출력 하려 고 할 때 사용 합 니 다.

//test1.php

<?php

include './tsest.php';

echo 'this is test1';

?>

 

//test2.php

<?php

echo 'this is test2
'; function test() { echo 'this is test
'; } ?> // : this is test1
require 불 러 오 는 데 실 패 했 을 때 치 명 적 인 오류 가 발생 합 니 다(ECOMPILE_ERROR)오류 가 발생 한 후 스 크 립 트 가 실행 을 중지 합 니 다.일반적으로 후속 코드 가 불 러 온 파일 에 의존 할 때 사용 합 니 다.

//test1.php

<?php

require './tsest.php';

echo 'this is test1';

?>

 

//test2.php

<?php

echo 'this is test2
'; function test() { echo 'this is test
'; } ?>
결과:

include 와 includeonce:
include 가 불 러 온 파일 은 중복 여 부 를 판단 하지 않 습 니 다.include 문장 만 있 으 면 한 번 불 러 옵 니 다(중복 불 러 올 수 있 더 라 도).그리고 includeonce 파일 을 불 러 올 때 내부 판단 메커니즘 이 있 습 니 다.여기 서 주의해 야 할 것 은 includeonce 는 앞에서 같은 경 로 를 도입 한 파일 이 있 는 지 없 는 지 에 따라 판단 합 니 다.파일 의 내용(즉,도입 할 파일 의 내용 이 같 으 면 include 를 사용 합 니 다.원 스 는 두 개 를 도입 한다).

//test1.php

<?php

include './test2.php';

echo 'this is test1';

include './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//  :

this is test2this is test1this is test2

 

 

//test1.php

<?php

include './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//  :

this is test2this is test1

 

 

//test1.php

<?php

include_once './test2.php';

echo 'this is test1';

include './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//  :

this is test2this is test1this is test2

 

 

//test1.php

<?php

include_once './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//  :

this is test2this is test1
require 와 requireonce:include 와 includeonce 의 차 이 는 같다.
이상 은 이번 소개 의 모든 지식 포인트 내용 입 니 다.여러분 의 지지 에 감 사 드 립 니 다.

좋은 웹페이지 즐겨찾기