OK, I know that I am being stupid about this, but I can't seem to figure out how I am being so.
I have variables for social media links in a separate .php file so that my client can change them without having to see too much code.
I include that .php file in my normal code where I want the links to be. But, for some reason the value of the variable is not being transmitted. I even tried to make a "global" variable. Here is my code.
File: 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';
?>
File: 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>
I ran a test like so:
<?php
if($facebook){
echo $facebook;
}else{
echo 'Facebook NULL';
}
?>
I put this after the include
statement and before the rest of the code. And it prints out 'Facebook NULL'. So, I know that the value is not being transmitted. Now, if I put in the included .php file (where the variables are stored) a echo "Hello World!";
line, "Hello World!" does print out on the screen. So, I know that the file is being included correctly (ie the path is correct).
I then placed this code after the 'include' and before the rest of the code:
$facebook = 'https://www.facebook.com/MenCoachingMen';
When I do that, the URL is included within the page. So, I know that my php statements within the code is correct. That means that it has to be a transmission of the value of the variable after including it. Please help. I know this must be a stupid mistake somewhere. Thank you.
A global variable must be used inside a function.
function name(){
global $globalvar = "some info";
}
to pass a variable OUT of it and into the script. You can't pass variables to other scripts using global variables. Consider using $_SESSION['name'] variables to pass variables from script to script.
<?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'; ?>
I hope this helps. Think of it like DIR and CD commands.