Định nghĩa và cách dùng include và require
include
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. 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 Warning: 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ấu trúc<?php require "đường_dẫn_file/tên_file"; ?> Ví dụ:<?php require "require/header.php"; ?> <div>Content</div> <?php require "require/footer.php"; ?> Nội dung 2 file header.php và footer.php đã được thêm vào. Download file ví dụ require 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: require(header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\example\index.php on line 1 Fatal error: require(): Failed opening required 'header.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\example\index.php on line 1 Ta thấy trình duyệt gửi cảnh báo, và những đoạn code bên dưới đã không được thực thi. PHP include và require với pathLưu ý: nếu chưa được cấu hình đường dẫn tương đối (relative path) thì dễ xảy ra lỗi khi
dùng Sử dụng đường dẫn tuyệt đốiSử dụng cách này cần kích hoạt allow_url_fopen và allow_url_include sang On trong php.ini <?php include "http://localhost/include/header.php"; ?> <div>Content</div> <?php include "http://localhost/include/footer.php"; ?> Download file ví dụ Sử dụng DOCUMENT_ROOT<?php include $_SERVER["DOCUMENT_ROOT"] . "/include/header.php"; ?> <div>Content</div> <?php include $_SERVER["DOCUMENT_ROOT"] . "/include/footer.php"; ?> Download file ví dụ Tác dụng của hàm array_pop()The The following table summarizes the technical details of this function.
SyntaxThe basic syntax of the The following example shows the
ParametersThe
More ExamplesHere're some more examples showing how This function can also be used with the associative array, as shown here:
Bài viết này đã giúp ích cho bạn? |