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

PHP current(): Returns The Current Element Of The Array

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.

  1. PHP current() Array Pointer:

    • Description: The current() function in PHP is used to return the current element in an array.
    • Example Code:
      $fruits = ['Apple', 'Banana', 'Orange'];
      echo current($fruits); // Outputs: Apple
      
  2. PHP current() vs reset() Function:

    • Description: While current() returns the current element, reset() resets the array's internal pointer to the first element.
    • Example Code:
      $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
      
  3. PHP current() Function with Associative Array:

    • Description: current() also works with associative arrays, returning the value of the current key-value pair.
    • Example Code:
      $person = ['name' => 'John', 'age' => 25, 'city' => 'New York'];
      echo current($person); // Outputs: John
      
  4. PHP current() Function Manual:

    • Description: The current() function is used to retrieve the value of the current array element.
    • Example Code:
      $array = [10, 20, 30];
      $currentValue = current($array);
      echo "Current Value: $currentValue"; // Outputs: Current Value: 10
      
  5. PHP current() Function Use Cases:

    • Description: current() is commonly used in loops to process elements of an array one at a time.
    • Example Code:
      $numbers = [1, 2, 3, 4, 5];
      while ($value = current($numbers)) {
          echo $value . ' ';
          next($numbers);
      }
      // Outputs: 1 2 3 4 5
      
  6. PHP current() Function Array End:

    • Description: current() can be used to get the last element of an array by moving the internal pointer to the end.
    • Example Code:
      $numbers = [1, 2, 3, 4, 5];
      end($numbers); // Move pointer to the end
      echo current($numbers); // Outputs: 5
      
  7. PHP Iterate Array with current():

    • Description: current() can be used in conjunction with loops to iterate through an array.
    • Example Code:
      $colors = ['Red', 'Green', 'Blue'];
      while ($color = current($colors)) {
          echo $color . ' ';
          next($colors);
      }
      // Outputs: Red Green Blue