PHP end() Function
In this chapter you will learn:
- Definition for PHP end() Function
- Syntax for PHP end() Function
- Parameter for PHP end() Function
- Related methods for PHP end() Function
- Note for Related methods
- Example - Goes to the end of an array
- Example - A demonstration of all related methods
Definition
The end() function moves the array pointer to the last element in the array and and outputs its value.
Syntax
PHP end() Function has the following syntax.
end(array)
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Specifies the array to use |
Related methods
Method | Description |
---|---|
current() | returns the value of the current element in an array |
next() | moves the pointer to, and outputs, the next element in the array |
prev() | moves the pointer to, and outputs, the previous 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 |
Note
The end()
function sets the array cursor to the last element and return that value.
If cannot return a value, it will return false.
Example 1
Goes to the end of an array
<?PHP// j a va 2s. 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
A demonstration of all related methods:
<?php//from j a v a 2 s . com
$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:
- Definition for PHP explode() Function
- Syntax for PHP explode() Function
- Parameter for PHP explode() Function
- Example - Turn a string value to an array
- Example - limit the number of elements in the returned array with a third parameter
Home » PHP Tutorial » PHP Array Functions