W3docs

prev()

What is a PHP Function?A PHP function is a block of code that can be called multiple times from different parts of a program. It performs a specific task, and

What is a PHP Function?

A PHP function is a block of code that can be called multiple times from different parts of a program. It performs a specific task, and may or may not require input parameters. PHP functions are useful for encapsulating logic and making code more modular and reusable.

Understanding the "prev" Function

The prev() function in PHP moves the internal array pointer one step backward and returns the value of the array element at the new position. It is useful when iterating over an array and you need to access the previous element. Because prev() modifies the array's internal pointer, it accepts the array by reference. The pointer must be positioned first, usually with reset(), before using prev(). The syntax is as follows:

prev(array &$array): mixed

The function takes an array by reference as its parameter and returns the previous value. If the pointer is already at the beginning of the array, prev() returns false.

Example Usage of the "prev" Function

Let's look at an example of how to use the prev() function in PHP. Suppose we have an array of numbers and we want to access the previous element:

<?php

$numbers = [5, 10, 15, 20, 25];
reset($numbers); // Position the pointer at the first element

echo "Current: " . current($numbers) . "\n";

while (next($numbers) !== false) {
    echo "Current: " . current($numbers) . "\n";

    $prev = prev($numbers); // Move pointer back and get the previous value
    if ($prev !== false) {
        echo "Previous: $prev\n";
    }

    next($numbers); // Move the pointer forward to continue the iteration
}

In this example, we initialize the array pointer with reset(). We then use current() to get the active element and next() to move through the array. After printing the current value, we call prev() to move the pointer back and retrieve the previous value. If the previous value is not false, we print it. Finally, we move the pointer forward with next() to continue the iteration.

Note: prev() returns false both when the pointer is at the beginning of the array and when the previous element's value is actually false. Use strict comparison (!== false) if you need to distinguish between them.

This code will output the following:


Current: 5
Current: 10
Previous: 5
Current: 15
Previous: 10
Current: 20
Previous: 15
Current: 25
Previous: 20

Conclusion

In this article, we covered the topic of PHP functions, with a focus on the prev() function. We provided an overview of what a PHP function is, explained the syntax and pointer-based usage of the prev() function, and provided a working example of how to use it in practice. We also highlighted how to handle edge cases where prev() returns false.

Diagram:


graph TD;
    A[Start] --> B[Initialize array with reset()];
    B --> C[Get current value with current()];
    C --> D[Move pointer forward with next()];
    D --> E[Call prev() to move pointer back];
    E --> F[Check if previous value is not false];
    F -->|Yes| G[Print previous value];
    G --> D;
    F -->|No| D;
    D -->|No more elements| I[End];

Practice

Practice

What does the PHP keyword 'dollar sign' denote in PHP?