0

I am call a function which is defined in my smsconfig.php file like

$GLOBAL_URL_OF_THE_SITE= "http://webfaction";


function get_site_url(){
    return $GLOBAL_URL_OF_THE_SITE; 
}

In my index.php file, I am including that smsconfig.php file then I am doing the following:

  <?php 
    include("smsconfig.php");
    ?>
    <html>
    <head >
     <link rel="stylesheet" type="text/css" href="default.css" />
        <script src="<?php echo get_site_url();?>/UI/jquery/jquery-1.7.2.min.js" type="text/javascript"></script>

unfortunately my JS is not loading and I am getting the following error in firebug:

"NetworkError: 404 Not Found - http://webfaction/UI/%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20GLOBAL_URL_OF_THE_SITE%20in%20%3Cb%3E/opt/lampp/htdocs/UI/smsconfig.php%3C/b%3E%20on%20line%20%3Cb%3E11%3C/b%3E%3Cbr%20/%3E/UI/jquery/jquery-1.7.2.min.js"

Please tell me what I am doing wrong ?

2
  • no no its nothing like that actually i make my 127.0.0.1 address as webfaction in /etc/host Commented Sep 25, 2012 at 14:02
  • You could just use constants to achieve that; look for define(). Commented Sep 25, 2012 at 14:05

3 Answers 3

4

Functions do not have access to global variables by default. You have to declare the variable with global before accessing it:

$GLOBAL_URL_OF_THE_SITE= "http://webfaction";

function get_site_url(){
    global $GLOBAL_URL_OF_THE_SITE;

    return $GLOBAL_URL_OF_THE_SITE; 
}

Without global, you should be seeing a warning, which you may be suppressing:

PHP Notice: Undefined variable: GLOBAL_URL_OF_THE_SITE

Sign up to request clarification or add additional context in comments.

1 Comment

The better solution is to avoid global variables in the first place. A few additional points: You shouldn't be embedding something like a variable's scope in its name, just call it $URL_OF_THE_SITE; You should choose shorter variable names... SITE_URL is shorter, easier to read and just as communicative as URL_OF_THE_SITE; You should reserve UPPER_CASE for true constants; if you're trying to define true constants, you should use define. This all adds up to define("SITE_URL", "http://mysite.com");
0

It's a problem with variable scope. Your function can't see the variable, because the the variable is outside its scope. You should do something like this:

function get_site_url(){
    $GLOBAL_URL_OF_THE_SITE= "http://webfaction";

    return $GLOBAL_URL_OF_THE_SITE; 
}

5 Comments

I guess it depends. If this is going to be used to generate the URL everywhere, it doesn't make any functional difference if you declare the URL inside this routine or globally. But yes, meager's solution is better.
what if i define the variable as a public
@user1667633 - have a look at meagar's comments.
@andrewsi My solution is not "my solution", I simply corrected the code we were given. Your solution is the better solution. There is no point in declaring both a global function and a global variable to access that function.
@user1667633 There is no such thing as a "public" variable outside of a class.
0

You may code without declaring global in function body by using array $GLOBALS. This array has all global variables as keys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.