PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The current()
function in PHP is used to get the value of the current element in an array.
Every array in PHP has an internal pointer that points to its "current" element. When an array is created, the internal pointer points to the first element of the array by default.
Here is an example of how to use current()
:
$fruits = ["Apple", "Banana", "Cherry"]; echo current($fruits); // Outputs: Apple
In this example, the current()
function returns the value of the first element in the array because the internal pointer is at the first element.
Moving the Internal Pointer
PHP provides several functions to move the internal pointer:
next()
: Move the internal pointer to the next element and return its value.prev()
: Move the internal pointer to the previous element and return its value.end()
: Move the internal pointer to the last element and return its value.reset()
: Move the internal pointer to the first element and return its value.Here's an example that shows how these functions work:
$fruits = ["Apple", "Banana", "Cherry"]; echo current($fruits) . "\n"; // Outputs: Apple echo next($fruits) . "\n"; // Outputs: Banana echo prev($fruits) . "\n"; // Outputs: Apple echo end($fruits) . "\n"; // Outputs: Cherry echo reset($fruits) . "\n"; // Outputs: Apple
In this example, next($fruits)
moves the internal pointer to the next element ("Banana"
) and returns its value. prev($fruits)
moves the pointer back to the first element ("Apple"
), and end($fruits)
moves the pointer to the last element ("Cherry"
). Finally, reset($fruits)
moves the pointer back to the first element.
PHP current()
Array Pointer:
current()
function in PHP is used to return the current element in an array.$fruits = ['Apple', 'Banana', 'Orange']; echo current($fruits); // Outputs: Apple
PHP current()
vs reset()
Function:
current()
returns the current element, reset()
resets the array's internal pointer to the first element.$fruits = ['Apple', 'Banana', 'Orange']; echo current($fruits); // Outputs: Apple next($fruits); // Move pointer to the next element echo current($fruits); // Outputs: Banana reset($fruits); // Reset pointer to the first element echo current($fruits); // Outputs: Apple
PHP current()
Function with Associative Array:
current()
also works with associative arrays, returning the value of the current key-value pair.$person = ['name' => 'John', 'age' => 25, 'city' => 'New York']; echo current($person); // Outputs: John
PHP current()
Function Manual:
current()
function is used to retrieve the value of the current array element.$array = [10, 20, 30]; $currentValue = current($array); echo "Current Value: $currentValue"; // Outputs: Current Value: 10
PHP current()
Function Use Cases:
current()
is commonly used in loops to process elements of an array one at a time.$numbers = [1, 2, 3, 4, 5]; while ($value = current($numbers)) { echo $value . ' '; next($numbers); } // Outputs: 1 2 3 4 5
PHP current()
Function Array End:
current()
can be used to get the last element of an array by moving the internal pointer to the end.$numbers = [1, 2, 3, 4, 5]; end($numbers); // Move pointer to the end echo current($numbers); // Outputs: 5
PHP Iterate Array with current()
:
current()
can be used in conjunction with loops to iterate through an array.$colors = ['Red', 'Green', 'Blue']; while ($color = current($colors)) { echo $color . ' '; next($colors); } // Outputs: Red Green Blue