Wordpress使用本地回退加载外部脚本

I can't figure out what I'm doing wrong! I want the function to return a freshly-created variable with a true/false value that I can use to see if we've got the file or not.

// Check to make sure external files are available
function checklink ( $link, $checkname ) { 
    $try_url = @fopen( $link,'r' );
    if( $try_url !== false ) { return $$checkname; } 
}

var_dump( checklink( 'https://code.jquery.com/jquery-3.3.1.min.js', 'jqueryOK' ) ); // NULL

I've tried setting $checkname to true or false, adding an extra line to give it a value before return ... PHP 'knows' there is a variable $jqueryOK but says it's undefined.

What am I missing?

UPDATE

Decided to share the outcome, as this is often an overlooked thing in Wordpress - and am changing the title to reflect the task.

// Check to make sure external files are available
function checklink ($link) { 
return( bool )@fopen( $link, 'r' );
}

function thatwomanuk_external_scripts()
{
if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) {

// jquery
    $link = 'https://code.jquery.com/jquery-3.3.1.min.js';
    if( checklink( $link ) ) { // true - otherwise, Wordpress will load its own     
        wp_deregister_script('jquery'); // remove jQuery v1
        wp_register_script('jquery', $link, array(), '3.3.1', true); // add jQuery v3
        wp_enqueue_script('jquery');
        wp_script_add_data( 'jquery', array( 'integrity', 'crossorigin' ), array( 'sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=', 'anonymous' ) );
    }   
// google fonts
    $link = 'https://fonts.googleapis.com/css?family=Raleway:300,400,400i,500|Ubuntu:300,400,400i,500&subset=latin-ext';
    $fallback = get_template_directory_uri() . '/fonts/thatwoman-fonts-min.css';
    if( checklink( $link ) ) {  
        wp_enqueue_style( 'custom-google-fonts', $link, false ); 
    } else {    
        wp_enqueue_style( 'custom-google-fonts', $fallback, false ); 
    }   
// touch events library
    $link = 'https://cdnjs.cloudflare.com/ajax/libs/jquery-touch-events/1.0.5/jquery.mobile-events.js';
    $fallback = get_template_directory_uri() . '/js/lib/jquery.mobile-events.min.js';
    if( checklink( $link ) ) {          
        wp_register_script('thatwoman-touch-events', $link, array( 'jquery' ), '1.0.5', true);
    } else {            
        wp_register_script('thatwoman-touch-events', $fallback, array( 'jquery' ), '1.0.5', true);
    }
    wp_enqueue_script('thatwoman-touch-events');
}
}
add_action( 'wp_enqueue_scripts', 'thatwomanuk_external_scripts' );

Thanks to @u_mulder for making me see sense.

You should simplify your function to:

// Check to make sure external files are available
function checklink ($link) { 
    return (bool)@fopen( $link,'r');
}

After that in your code:

$link1Available = checklink($link1);
$link2Available = checklink($link2);
// etc

Or as an array:

$links = ['link1', 'link2', 'link3'];
$linksAvailable = [];
foreach ($links as $link) {
    $linksAvailable[$link] = checklink($link);
}

You have double $$ sign in return statement:

if( $try_url !== false ) { return $$checkname; } 

If you just want to have a local fallback for a CDN loaded js file, you can use wp_scripts()->add_inline_script() which was added in 4.5, to add a js check if the library was loaded, see my detailed answer on wordpress.stackexchange here.

The short version:

You can use wp_scripts()->add_inline_script(), which will append a js script to an enqueued script, no questions asked, no code problems checked (there is a wrapper function wp_add_inline_script that does checks but does not permit </script> tags, even encoded).

This is a working example

function _enqueue_my_scripts() {
  // the normal enqueue
  wp_enqueue_script( 'jquery-cdn', 'https://code.jquery.com/jquery-3.3.1.min.js' );
  // our custom code which will check for the existance of jQuery (in this case)
  wp_scripts()->add_inline_script( 'jquery-cdn',
    "window.jQuery || document.write('<script src=\"".get_template_directory_uri() ."/js/jQuery.js\">\\x3C/script>')", 'after');
}
add_action('wp_enqueue_scripts', '_enqueue_my_scripts', 100);

which will give you

<script type='text/javascript' src='https://code.jquery.com/jquery-3.3.1.min.js'></script>
<script type='text/javascript'>
window.Swiper || document.write('<script src="http://localhost/wordpress/wp-content/themes/wp-theme/js/jQuery.js">\x3C/script>')
</script>

in your HTML.

Replace 'window.jQuery' and '/js/jQuery.js' according to your needs but don't touch the rest of the line as it is meant to pass through the correct line to the HTML.