What is the difference between using $wpdb->insert() in a function and outside of a function in WordPress?

When using $wpdb->insert() outside of a function in WordPress, you can directly call the global $wpdb object. However, when using it inside a function, you need to declare global $wpdb; at the beginning of the function to access the $wpdb object. This ensures that the $wpdb object is available within the function scope.

// Using $wpdb->insert() outside of a function
global $wpdb;
$wpdb->insert( 
    'table_name', 
    array( 
        'column1' => 'value1', 
        'column2' => 'value2' 
    ) 
);

// Using $wpdb->insert() inside a function
function insert_data_function() {
    global $wpdb;
    $wpdb->insert( 
        'table_name', 
        array( 
            'column1' => 'value1', 
            'column2' => 'value2' 
        ) 
    );
}