What are the best practices for handling global variables like $wpdb in PHP scripts?

When handling global variables like $wpdb in PHP scripts, it is important to avoid directly accessing them outside of the WordPress environment to prevent security vulnerabilities and ensure compatibility with future updates. To address this, it is recommended to use the WordPress function get_option('siteurl') to retrieve the database connection details and then establish a new database connection using the wpdb class.

// Get the database connection details using WordPress function
$db_host = get_option('siteurl');
$db_name = get_option('siteurl');
$db_user = get_option('siteurl');
$db_pass = get_option('siteurl');

// Establish a new database connection using wpdb class
$new_db = new wpdb($db_user, $db_pass, $db_name, $db_host);

// Now you can use $new_db for database operations instead of $wpdb