Hướng dẫn dùng pop value trong 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 include và require:

    Nội dung chính

    • Định nghĩa và cách dùng include và require
    • PHP include và require với path
    • Tác dụng của hàm array_pop()
    • More Examples
    • Bài viết này đã giúp ích cho bạn?

    • 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.
    • 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.

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.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

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 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ụ:

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

Nội dung 2 file header.phpfooter.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 path

Lư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
/đườ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:

Sử dụng đường dẫn tuyệt đối

Sử 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 array_pop() function removes the last element of an array.

The following table summarizes the technical details of this function.

Return Value:Returns the value of the last element of array.
If array is empty or is not an array, NULL will be returned.
Version:PHP 4+

Syntax

The basic syntax of the array_pop() function is given with:

The following example shows the array_pop() function in action.

<?php
// Sample array
$cities = array("London", "Paris", "New York", "Sydney");
    
// Remove and get the last value from array
echo array_pop($cities); // Prints: Sydney
print_r($cities);
?>

Parameters

The array_pop() function accepts the following parameters.

ParameterDescription
array Required. Specifies the array to get the last value from.

More Examples

Here're some more examples showing how array_pop() function actually works:

This function can also be used with the associative array, as shown here:

<?php
// Sample array
$alphabets = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");
    
// Remove and get the last value from array
echo array_pop($alphabets); // Prints: dog
print_r($alphabets);
?>

Bài viết này đã giúp ích cho bạn?