Assign output of PHP function to a variable



PHP Snippet 1:

function get_coins($conn): array {
    return $sql
        ->query("SELECT id,coin,cap
                 FROM marketcaps
                 ORDER BY cap DESC
                 LIMIT 25")
        ->fetch_all(MYSQLI_ASSOC);
}

foreach (get_coins($conn) as $row) {
    // ... reference $row['id'] , $row['coin'] , $row['cap'] during your processes
}

PHP Snippet 2:

function get_coins()
{
    global $conn;
    $coins = array();
    $new_sql = "SELECT * FROM marketcaps ORDER BY cap DESC LIMIT 25";
    if( $new_result = $conn->query($new_sql) )
    {
        while( $row = $new_result->fetch_assoc() )
        {
            $coins[ $row["id"] ] = $row["coin"] . '~USD,';
        }
    }
    return ( !empty( $coins ) ) ? $coins : null;
}