Hướng dẫn dùng pos 99999999 trong PHP

Hàm pos() trong PHP dùng để lấy ra phần tử mà con trỏ nội bộ của mảng đang trỏ vào.

Nội dung chính

  • Cùng chuyên mục:
  • Định nghĩa hàm pos() trong PHP
  • Cú pháp hàm pos() trong PHP
  • Ví dụ minh họa cách sử dụng hàm pos() trong PHP:__
  • Tác dụng của hàm pos()
  • More Examples
  • Bài viết này đã giúp ích cho bạn?
  • Bài viết mới

Hướng dẫn dùng pos 99999999 trong PHP

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháppos( $array);

Trong đó:

  • $array là mảng cần lấy ra phần tử.

Ví dụ

Code

$arr = array(
	'vi tri 1',
	'vi tri 2' ,
	'vi tri 3',
	'vi tri 4'
);
echo pos($arr).'
'; next($arr); echo pos($arr).'
';

Kết quả

vi tri 1
vi tri 2

Mặc định ban đầu, con trỏ sẽ ở vị trí phần tử đầu tiên của mảng(vi tri 1), sau khi sử dụng hàm next() con trỏ nội bộ sẽ ở vị trí thứ 2(vi tri 2).

Bài viết này được đăng tại [free tuts .net]

Tham khảo: php.net

Cùng chuyên mục:

Định nghĩa hàm pos() trong PHP

Hàm pos() trong PHP là một alias của hàm current() trong PHP và 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ó không di chuyển con trỏ nội tại này. Nếu con trỏ này trỏ tới cuối danh sách phần tử, hàm post() này trả về FALSE.

Cú pháp hàm pos() trong PHP

Hàm pos() trong PHP có cú pháp như sau:

pos ( $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 pos() 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ỉ http://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 pos() trong PHP

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

Tác dụng của hàm pos()

The pos() function returns the value of the current element in an array (alias of current() function).

The following table summarizes the technical details of this function.

Return Value: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.
Changelog: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.
Version: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 reference the first element in the array. The pos() function does not move the arrays internal pointer in any way.


Syntax

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

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


Parameters

The pos() function accepts the following parameters.

ParameterDescription
array Required. Specifies the array to work on.

More Examples

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

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

"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");

// Getting the current element's value
echo pos($alphabets); // Prints: apple

// Getting the current element's key
echo key($alphabets); // Prints: a
?>

The pos() 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:

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

Bài viết mới