PHP | current()函数

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

null
  • 它用于返回内部指针当前指向的数组中元素的值。
  • current()函数 不增加或减少 返回值后的内部指针。
  • 在PHP中,所有数组都有一个内部指针。这个内部指针指向数组中的某个元素,该元素被称为数组的当前元素。
  • 通常,当前元素是数组中插入的第一个元素。

语法:

current($array)

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

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

例如:

Input : current(array("John", "b", "c", "d"))Output : JohnExplanation : Here as we see that input array contains many elements and the output is "John" because first element is John and current() function returns the element to which internal pointer is currentlypointing.Input: current(array("abc", "123", "7"))Output: abc

下面的程序演示了PHP中的current()函数:

方案1 :

PHP

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


输出:

Ram

方案2 :

PHP

<?php
$arr = array ( 'Sham' , 'Mac' , 'Jhon' , 'Adwin' );
// Here current element is Sham.
echo current( $arr ). "" ;
// increment internal pointer to point
// to next element i.e, Mac
echo next( $arr ). "" ;
// printing the current element as
// for now current element is Mac.
echo current( $arr ). "" ;
// increment internal pointer to point
// to next element i.e, Jhon.
echo next( $arr ). "" ;
// increment internal pointer to point
// to next element i.e, Adwin.
echo next( $arr ). "" ;
// printing the current element as for
// now current element is Adwin.
echo current( $arr ). "" ;
?>


输出:

ShamMacMacJhonAdwinAdwin

注: 当数组为空(即不包含任何元素)时,current()函数返回False;当内部指针超出边界(即超出最后一个元素的末尾)时,函数也返回False。

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

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享