Hướng dẫn why is php required? - tại sao lại yêu cầu php?

  • Trang chủ
  • Hướng dẫn học
  • Học PHP
  • PHP include và require

Định nghĩa và cách dùng include và require

  • include hoặc require tiện lợi cho việc sử dụng những phần dùng chung, ví dụ header, footer, sidebar hoặc một function dùng chung nào đó.
  • Đối với những phần dùng chung này, ta tách riêng ra 1 file PHP, sau đó sử dụng include hoặc require để kết nối.
  • Điều này tiện lợi cho việc chỉnh sửa, thay vì chỉnh nhiều trang, giờ thì ta chỉ việc chỉnh file PHP đã tách riêng là được.
  • Sự khác biệt giữa includerequire:

    • include: code bên dưới include sẽ tiếp tục thực thi, cho dù file được include có tồn tại hay không.tiếp tục thực thi, cho dù file được include có tồn tại hay không.
    • require: code bên dưới require sẽ không được thực thi, nếu file được require không tồn tại.không được thực thi, nếu file được require không tồn tại.

include

  • Được dùng để chèn một file PHP vào một file PHP khác.

Cấu trúc

<?php
include "đường_dẫn_file/tên_file";
?>

Ví dụ:

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>

Ta thấy nội dung 2 file header.php và footer.php đã được thêm vào.header.phpfooter.php đã được thêm vào.

Download file ví dụ

include với file không tồn tại

<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>

Warning: include(header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\example\index.php on line 1: include(header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\example\index.php on line 1

Warning: include(): Failed opening 'header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\example\index.php on line 1: include(): Failed opening 'header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\example\index.php on line 1

Content

Ta thấy trình duyệt gửi cảnh báo, tuy nhiên những PHP vẫn thực thi những đoạn code bên dưới.

require

  • Được dùng để chèn một file PHP vào một file PHP khác, file được chèn bắt buộc phải tồn tại, nếu không sẽ không thực thi những đoạn code tiếp theo.
  • Thường được dùng để chèn nội dung kết nối database, function login, payment, ...
  • Cách sử dụng tương tự như include.

Cấu trúc

<?php
require "đường_dẫn_file/tên_file";
?>

Ví dụ:

Ta thấy nội dung 2 file header.php và footer.php đã được thêm vào.

Download file ví dụheader.phpfooter.php đã được thêm vào.

Download file ví dụ

include với file không tồn tại

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>

Warning: include(header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\example\index.php on line 1: require(header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\example\index.php on line 1

Warning: include(): Failed opening 'header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\example\index.php on line 1: require(): Failed opening required 'header.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\example\index.php on line 1

Content

Ta thấy trình duyệt gửi cảnh báo, tuy nhiên những PHP vẫn thực thi những đoạn code bên dưới.

require
/đường_dẫn_file/tên_file (trong trường hợp phân cấp thư mục), khắc phục tình trạng này có 2 cách:

Được dùng để chèn một file PHP vào một file PHP khác, file được chèn bắt buộc phải tồn tại, nếu không sẽ không thực thi những đoạn code tiếp theo.

Thường được dùng để chèn nội dung kết nối database, function login, payment, ...

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>

Download file ví dụ

include với file không tồn tại

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>

Download file ví dụ

(Php 4, Php 5, Php 7, Php 8)

require giống hệt nhau bao gồm ngoại trừ khi thất bại, nó cũng sẽ tạo ra lỗi mức

<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
5 gây tử vong. Nói cách khác, nó sẽ tạm dừng tập lệnh trong khi chỉ bao gồm chỉ phát ra cảnh báo (
<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
6) cho phép tập lệnh tiếp tục.include except upon failure it will also produce a fatal
<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
5
level error. In other words, it will halt the script whereas include only emits a warning (
<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
6
) which allows the script to continue.

Xem tài liệu bao gồm cách thức hoạt động của nó.include documentation for how this works.

Chris tại Chrisstockton Dot org ¶

15 năm trước

<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
7

Marcel Baur ¶

1 năm trước

<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
8

<?php include "include/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php include "include/footer.php"; ?>
9

<?php
require "đường_dẫn_file/tên_file";
?>
0

Osheros tại hotmail dot com

2 tháng trước đây

<?php
require "đường_dẫn_file/tên_file";
?>
1

<?php
require "đường_dẫn_file/tên_file";
?>
2

<?php
require "đường_dẫn_file/tên_file";
?>
3

<?php
require "đường_dẫn_file/tên_file";
?>
4

<?php
require "đường_dẫn_file/tên_file";
?>
5

<?php
require "đường_dẫn_file/tên_file";
?>
0

Ẩn danh ¶

15 năm trước

<?php
require "đường_dẫn_file/tên_file";
?>
7

<?php
require "đường_dẫn_file/tên_file";
?>
8

<?php
require "đường_dẫn_file/tên_file";
?>
9

<?php
require "đường_dẫn_file/tên_file";
?>
0

tjeerd ¶

16 năm trước

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
1

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
2

<?php
require "đường_dẫn_file/tên_file";
?>
0

Dank tại Kegel Dot Com ¶

7 năm trước

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
4

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
5

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
6

<?php
require "đường_dẫn_file/tên_file";
?>
0

Cánh ¶

12 năm trước

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
8

<?php require "require/header.php"; ?>
<div>Content</div>
<?php require "require/footer.php"; ?>
9

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
0

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
1

<?php
require "đường_dẫn_file/tên_file";
?>
0

Richardbrenner (-at-) gmx (-) tại ¶

17 năm trước

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
3

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
4

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
5

<?php
require "đường_dẫn_file/tên_file";
?>
0

phpdev a t esurfers_dot_ com ¶

6 năm trước

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
7

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
8

<?php require "require/header.php"; ?>  // Giả sử file header.php không có
<div>Content</div>
<?php require "require/footer.php"; ?>
9

<?php
require "đường_dẫn_file/tên_file";
?>
0

Một số người dùng chấm tại notarealdomain dot com

15 năm trước

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
1

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
2

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
3

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
4

<?php
require "đường_dẫn_file/tên_file";
?>
0

quản trị trang web tại NetGeekz Dot Net

16 năm trước

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
6

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
7

<?php include "http://localhost/include/header.php"; ?>
<div>Content</div>
<?php include "http://localhost/include/footer.php"; ?>
8

<?php
require "đường_dẫn_file/tên_file";
?>
0

Dank tại Kegel Dot Com ¶

15 năm trước

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
0

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
1

<?php
require "đường_dẫn_file/tên_file";
?>
0

7 năm trước

Cánh ¶

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
3

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
4

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
5

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
6

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
7

<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?>
<div>Content</div>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?>
8

<?php
require "đường_dẫn_file/tên_file";
?>
0

12 năm trước

17 năm trước

include0

include1

include2

<?php
require "đường_dẫn_file/tên_file";
?>
0

phpdev a t esurfers_dot_ com ¶

16 năm trước

include4

include5

<?php
require "đường_dẫn_file/tên_file";
?>
0

Dank tại Kegel Dot Com ¶

16 năm trước

include7

include8

include9

require0

require1

require2

require3

require4

require5

require6

<?php
require "đường_dẫn_file/tên_file";
?>
0

Dank tại Kegel Dot Com ¶

16 năm trước

require8

require9

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
00

<?php
require "đường_dẫn_file/tên_file";
?>
0

Ẩn danh ¶

17 năm trước

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
02

phpdev a t esurfers_dot_ com ¶

16 năm trước

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
03

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
04

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
05

<?php
require "đường_dẫn_file/tên_file";
?>
0

Dank tại Kegel Dot Com ¶

7 năm trước

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
07

Cánh ¶

Cánh ¶

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
08

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
09

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
10

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
11

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
12

12 năm trước

Richardbrenner (-at-) gmx (-) tại ¶

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
13

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
14

<?php
require "đường_dẫn_file/tên_file";
?>
0

17 năm trước

15 năm trước

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
16

<?php include "include/header.php"; ?>
<div>Content</div>
<?php include "include/footer.php"; ?>
17

<?php
require "đường_dẫn_file/tên_file";
?>
0