0

I face a strange problem including php files. Let me show you the code:

// constants.php
$MYSQL_HOST_PORT = 'localhost:3306';

// functions.php
include 'constants.php';
function getVar()  {
    echo $MYSQL_HOST_PORT;
}

// doSth.php
include 'functions.php';
echo $MYSQL_HOST_PORT; // The variable is visible and echoed normally as expected!
echo getVar(); // The variable is not echoed! its "".

Any ideas ?

2
  • 1
    This is a scope issue, not an include issue. Functions have their own scope in PHP. See php.net/manual/en/language.variables.scope.php Commented Apr 11, 2011 at 0:17
  • 1
    OK! Its a scope issue! You see I m coming from c++, obj-c, etc! Thank you! Solved. Commented Apr 11, 2011 at 1:07

3 Answers 3

1

For one, the echo in echo getVar(); won't ever print anything, because getVar doesn't return a value.

Secondly, if you (for some reason) want getVar() itself to work correctly, you need to add a global $MYSQL_HOST_PORT; line, to make it look for $MYSQL_HOST_PORT in the global scope.

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

5 Comments

Were getVar() to actually echo something besides an unset variable, running echo getVar() would still result in something being echo'd though the function name is misleading.
echo getVar() would echo something, but that specific echo wouldn't be causing any output.
this is an example of my problem. Even if do return $MYSQL_HOST_PORT the problem will still remain.
See the second part of my answer, or Phil Brown's answer. You're declaring $MYSQL_HOST_PORT in the global scope. Normally, a function will only "see" variables that are declared in its own local scope, so as far as getVar() is concerned, $MYSQL_HOST_PORT doesn't exist. One way around this is to add a global declaration for that variable inside the function, so PHP will look for it in the global scope. (Avoiding the global variable entirely would probably be preferable, though).
Thanks... didn't know about that scope issue - new to php and little strange for me!
1

Rather than globalising the $MYSQL_HOST_PORT variable, why not simply make it a constant?

// constants.php
define('MYSQL_HOST_PORT', 'localhost:3306');

Provided constants.php is included, you can reference the MYSQL_HOST_PORT constant anywhere.

As indicated in zerocrate's answer, the issue is a scoping one. The enclosed scope of the getVar() function does not include $MYSQL_HOST_PORT.

2 Comments

+1, or make it an environment variable to put in the webserver config, and reference it using getenv().
I will use the define finally! Thanks
-1

One thing that I can see wrong is that with the line echo getVar(); you are not getting a return value from the function so you can simply write getVar(); by itself.

1 Comment

The $MYSQL_HOST_PORT variable is not in scope of the getVar() function so it's not ever going to work as-is.

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.