How does using the SQL variant for substring extraction compare to using PHP functions like substr in terms of performance?

When extracting substrings from a string, using SQL functions like SUBSTRING may be more efficient than using PHP functions like substr, especially when dealing with large datasets. This is because SQL can directly operate on the database server, reducing the amount of data transferred between the server and the application. However, the choice between SQL and PHP functions should also consider factors like code readability and maintainability.

// Using SQL variant for substring extraction
$query = "SELECT SUBSTRING(column_name, start_position, length) AS substring FROM table_name WHERE condition";
$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['substring'] . "<br>";
}