不使用include语句传输的PHP变量的值


Value of PHP variables not transferring with include statement

好吧,我知道我在这件事上很愚蠢,但我似乎不明白我是怎么做到的。

我在一个单独的.php文件中有社交媒体链接的变量,这样我的客户就可以在不需要查看太多代码的情况下更改它们。

我在我的普通代码中包含了php文件,我希望链接在那里。但是,由于某种原因,变量的值没有被传输。我甚至试图制作一个"全局"变量。这是我的密码。

文件:socialMediaURLs.php

<?php
    global $facebook = 'https://www.facebook.com/MenCoachingMen'; 
    $googlePlus = 'https://plus.google.com/104275309033865331192/posts';
    $twitter = 'https://twitter.com/MenCoachingMen';
    $rss = 'http://mencoachingmen.org/category/podcast/feed/';
    $vimeo = 'https://vimeo.com/mencoachingmen';
    $youtube = 'https://www.youtube.com/channel/UCy_Pth5x-O7rcX9nMx1e8qw';
?>

文件:socialLinks.php

<?php include './socialMediaURLs.php'; ?>
<div class="social_links_wrapper">
    <a href="<?php echo $facebook;?>">
        <div class="sl_facebook">
            <i class="fa fa-facebook fa-5x"></i>
        </div>
    </a>
    <a href="<?php echo $googlePlus;?>">
        <div class="sl_googlePlus">
            <i class="fa fa-google-plus fa-5x"></i>
        </div>
    </a>    
    <a href="<?php echo $twitter;?>">
        <div class="sl_twitter">
            <i class="fa fa-twitter fa-5x"></i>
        </div>
    </a>
    <a href="<?php echo $rss;?>">
        <div class="sl_rss">
            <i class="fa fa-rss fa-5x"></i>
        </div>
    </a>
    <a href="<?php echo $vimeo;?>">
        <div class="sl_vimeo">
            <i class="fa fa-vimeo-square fa-5x"></i>
        </div>
    </a>
    <a href="<?php echo $youtube;?>">
        <div class="sl_youtube">
            <i class="fa fa-youtube fa-5x"></i>
        </div>
    </a>
</div>

我做了一个这样的测试:

<?php 
    if($facebook){
        echo $facebook;
    }else{
        echo 'Facebook NULL';
    }
?>

我把它放在include语句之后,放在代码的其余部分之前。它会打印出"Facebook NULL"。所以,我知道价值没有被传递。现在,如果我在包含的.php文件(存储变量的地方)中放入一行echo "Hello World!";,"Hello World!"就会在屏幕上打印出来。因此,我知道文件被正确地包括在内(即路径是正确的)。

然后我把这个代码放在"include"之后,放在代码的其余部分之前:

$facebook = 'https://www.facebook.com/MenCoachingMen'; 

当我这样做时,URL会包含在页面中。因此,我知道代码中的php语句是正确的。这意味着它必须是包含变量后的值的传输。请帮助。我知道这一定是个愚蠢的错误。非常感谢。

函数内部必须使用全局变量。

function name(){
    global $globalvar = "some info";
}

将变量OUT从中传递到脚本中。不能使用全局变量将变量传递给其他脚本。考虑使用$_SESSION['name']变量在脚本之间传递变量。

http://php.net/manual/en/book.session.php

<?php include 'socialMediaURLs.php'; ?> <--- top main public_html/www
<?php include 'directory1/socialMediaURLs.php'; ?> <-- one folder deep
<?php include 'directory1/directory2/socialMediaURLS.php'; ?> <-- two folders deep
//calling a file FROM two folders deep TO A FILE IN the root folder!    
<?php include('../../socialMediaURLS.php'; ?>  <--- main index public_html/www
//ETC......
<?php include('../../directory1/socialMediaURLS.php'; ?>

我希望这能有所帮助。把它想象成DIR和CD命令。