PHP prev() Function

In this chapter you will learn:

  1. Definition for PHP prev() Function
  2. Syntax for PHP prev() Function
  3. Parameter for PHP prev() Function
  4. Related methods for PHP prev() Function
  5. Example - Goes to the previous one in an array
  6. Example - Holes in Arrays
  7. Example - Output the value of the current, next and previous element in the array
  8. Example - A demonstration of all related methods

Definition

The prev() function moves the internal pointer to the previous element in the array and output its value.

Syntax

PHP prev() Function has the following syntax.

prev(array)

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to use
MethodDescription
next()moves the pointer to, and outputs the next element in the array
current()returns the value of the current element in an array
end()moves the pointer to, and outputs the last element in the array
reset()moves the pointer to the first element of the array
each()returns the current element key and value, and moves the pointer forward

Example 1

The next() and prev() functions move the cursor pointer forward or backward one element respectively, returning the value of the element now pointed to.

If cannot return a value, it will return false.


<?PHP/* j a  va2  s.co  m*/
$array = array("A", "B", "C", "D", "E");
print end($array);

while($val = prev($array)) {
   print $val;
}
?>

The code above generates the following result.

Example 2

Using prev() and next() is more difficult when using arrays that have holes. For example:


<?PHP// j  ava 2s .c om
$array["a"] = "Foo"; 
$array["b"] = ""; 
$array["c"] = "Baz"; 
$array["d"] = "Wom"; 
print end($array); 

while($val = prev($array)) { 
        print $val; 
} 
?>

The empty value at key b causes both prev() and next() to return false, prematurely ending the loop.

The code above generates the following result.

Example 3

Output the value of the current, next and previous element in the array:


<?php/* j a  v  a2  s. c o m*/
$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo current($people) . "\n";
echo next($people) . "\n";
echo prev($people);
?>

The code above generates the following result.

Example 4

A demonstration of all related methods:


<?php//j  a v  a  2 s  .c o  m
$people = array("A", "B", "C", "D");

echo current($people) . "\n"; // The current element is A
echo next($people) . "\n"; // The next element of A is B
echo current($people) . "\n"; // Now the current element is B
echo prev($people) . "\n"; // The previous element of B is A
echo end($people) . "\n"; // The last element is D
echo prev($people) . "\n"; // The previous element of D is C
echo current($people) . "\n"; // Now the current element is C
echo reset($people) . "\n"; // Moves the internal pointer to the first element of the array, which is A
echo next($people) . "\n"; // The next element of A is B

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

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP range() Function
  2. Syntax for PHP range() Function
  3. Parameter for PHP range() Function
  4. Note for PHP range() Function
  5. Return value for PHP range() Function
  6. Example - Use range() function to create array
  7. Example - Letter range
  8. Example - Create an array containing a range of elements from "0" to "5"
  9. Example - Return an array of elements from "0" to "50" and increment by 10
  10. Example - Using letters - return an array of elements from "a" to "d"
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function