当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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:

<?php 
  
// input array  
$arr = array("Ram", "Shita", "Geeta"); 
  
// Here current function returns the 
//  first element of the array. 
echo current($a); 
  
?>

输出:

Ram

程序2:

<?php 
  
$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。

参考:
http://php.net/manual/en/function.current.php



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 PHP | current() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。