Latest web development tutorials

PHP current () function

PHP Array Reference Complete PHP Array Reference

Examples

Output current array element values:

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo current($people) . "<br>";
?>

Running instance »

Definition and Usage

current () function returns the value of the current element in the array.

Each array has an internal pointer to its "current" element, the initial point to insert into the array's first element.

Tip: This function does not move the internal array pointer.

Related methods:

  • End () - the internal pointer to the last element in the array, and output.
  • Next () - the internal pointer to the next element in the array, and outputs.
  • the PREV () - the internal pointer to the last element of the array, and output.
  • RESET () - the internal pointer to the first element in the array, and output.
  • the each () - Returns the current element's key names and values, and the internal pointer moves forward.

grammar

current( array )

参数 描述
array 必需。规定要使用的数组。

technical details

return value: Returns the value of the current element in the array, if the current element is empty or not the current element value is returned FALSE.
PHP version: 4+


More examples

Example 1

All relevant presentation methods:

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo current($people) . "<br>"; // The current element is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
echo current($people) . "<br>"; // Now the current element is Joe
echo prev($people) . "<br>"; // The previous element of Joe is Peter
echo end($people) . "<br>"; // The last element is Cleveland
echo prev($people) . "<br>"; // The previous element of Cleveland is Glenn
echo current($people) . "<br>"; // Now the current element is Glenn
echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe

print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward
?>

Running instance »


PHP Array Reference Complete PHP Array Reference