Hướng dẫn dùng current current trong PHP

Định nghĩa hàm current[] trong PHP

Mỗi mảng có một con trỏ nội tại tới phần tử "hiện tại" của nó, mà được khởi tạo tới phần tử đầu tiên được chèn vào mảng.

Về cơ bản, hàm current[] trả về value của phần tử mảng mà hiện tại đang được trỏ bởi con trỏ nội tại này. Hàm này không di chuyển con trỏ nội tại này. Nếu con trỏ nội bộ chỉ đến phần tử cuối cùng trong mảng, thì hàm current[] trả về FALSE.

Cú pháp hàm current[] trong PHP

Dưới đây là cú pháp của hàm current[] trong PHP:

current [ $array ];

Tham số

array: Bắt buộc. Xác định một mảng

Trả về giá trị

Trả về phần tử hiện tại trong một mảng.

Ví dụ minh họa cách sử dụng hàm current[] trong PHP:

 

Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ //localhost:8080/test.php sẽ cho kết quả:

Xem thêm Hàm trong php

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm current[] trong PHP

Hướng dẫn cách sử dụng hàm current[] về mảng trong lập trình PHP

Tác dụng của hàm current[]

The current[] function returns the value of the current element in an array.

The following table summarizes the technical details of this function.

Return Value:Changelog:Version:
Returns the value of the current element in an array. Returns FALSE if the array is empty or array's internal pointer points beyond the end of the elements list.
Since PHP 7.0.0, array is always passed by value to this function. Prior to this version, it was passed by reference if possible, and by value otherwise.
PHP 4+

Tip: Every array has an internal pointer that points to the current element in the array. When a new array is created, the current pointer is initialized to reference the first element in the array. The current[] function does not move the arrays internal pointer in any way.

Syntax

The basic syntax of the current[] function is given with:

The following example shows the current[] function in action.

Parameters

The current[] function accepts the following parameters.

ParameterDescription
array Required. Specifies the array to work on.

More Examples

Here're some more examples showing how current[] function actually works:

The following example demonstrates how to get the current value from an associative array:

The current[] function is commonly used along with the following functions:

  • prev[] – Moves the internal pointer of an array to the previous element, and returns its value.
  • next[] – Moves the internal pointer of an array to the next element, and returns its value.
  • end[] – Moves the internal pointer of an array to its last element, and returns its value.
  • reset[] – Set the internal pointer of an array to its first element, and returns its value.
  • key[] – Returns the key of the current element in an array.

Here's an example that demonstrates how these functions basically work:

Chủ Đề