php的current和pos,PHP current()用法及代码示例

current()函数是PHP中的内置函数。

它用于返回内部指针当前指向的数组中元素的值。

返回值后,current()函数不会递增或递减内部指针。

在PHP中,所有数组都有一个内部指针。此内部指针指向该数组中的某个元素,该元素称为数组的当前元素。

通常,当前元素是数组中第一个插入的元素。

用法:

current($rray)

参数:current()函数接受单个参数$array。这是我们要查找当前元素的数组。

Return Values:它返回内部指针当前指向的数组中元素的值。如果数组为空,则current()函数将返回FALSE。

例子:

Input : current(array("John", "b", "c", "d"))

Output : John

Explanation : Here as we see that input array contains

many elements and the ouput is "John" because first

element is John and current() function returns

the element to which internal pointer is currently

pointing.

Input: current(array("abc", "123", "7"))

Output: abc

以下示例程序旨在说明PHP中的pos()函数:

程序1::

// input array

$arr = array("Ram", "Shita", "Geeta");

// Here current function returns the

//  first element of the array.

echo current($a);

?>

输出:

Ram

程序2::

$arr = array('Sham', 'Mac', 'Jhon', 'Adwin');

// Here current element is Sham.

echo current($arr)."\n";

// increment internal pointer to point

// to next element i.e, Mac

echo next($arr)."\n";

// printing the current element as

// for now current element is Mac.

echo current($arr)."\n";

// increment internal pointer to point

// to next element i.e, Jhon.

echo next($arr)."\n";

// increment internal pointer to point

// to next element i.e, Adwin.

echo next($arr)."\n";

// printing the current element as for

// now current element is Adwin.

echo current($arr)."\n";

?>

输出:

Sham

Mac

Mac

Jhon

Adwin

Adwin

注意:当数组为空(即不包含任何元素)时,current()函数返回False,并且当内部指针超出范围(即最后一个元素的末尾)时,它返回false。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值