Jump to content

Some issue while defining variable


[dx]

Recommended Posts

Hi, I have issue with this code. In my included php file there's var $something = 'x'; and in tpl file there's <?php echo $something; ?> But variable seems not to be defined.

$page = (isset($_GET['page']) ? $_GET['page'] : 'site'); $option = (isset($_GET['option']) ? $_GET['option'] : 'main'); $allowed_pages = array('admin', 'site'); function func_controller($allowed_pages, $page = NULL, $option = NULL) {  if (in_array($page, $allowed_pages)) {    func_require('/controller/'.$page.'/'.$option.'.php');  }}function func_template($allowed_pages, $page = NULL, $option = NULL) {  if (in_array($page, $allowed_pages)) {    func_require('/view/default/'.$page.'/'.$option.'.tpl');  }} func_controller($allowed_pages, $page, $option);func_template($allowed_pages, $page, $option); var_dump(get_included_files());var_dump(get_defined_vars());

Best regards.

Edited by Haris S
Link to comment
Share on other sites

The file is being included from inside a function, which means the code inside that included file is being executed in the scope of that function, which means that you need to define any global variables you want to use in the included code as global. It's the same issue as if the code was copied and pasted into that function instead of being included, the variables are not global inside the function.

Link to comment
Share on other sites

But what if I use only outside functions, it's same.. not defined variable

func_require('/controller/'.$page.'/'.$option.'.php');func_require('/view/default/'.$page.'/'.$option.'.tpl');

Link to comment
Share on other sites

I believe creating a global variable in one script makes it accessible to any script in your directory...

$var = 1;  function your_function(){      global $var;    //now you can use this variable within your function.}

Regards, Labtec.

Link to comment
Share on other sites

You can refer to them using the $GLOBALS array. If you're going to use global variables inside functions then you need to say they're global, there's not a way around that. You can use static classes to replace global variables, but if you insist on using global variables then you need to say that they are global. If you don't want to say that each variable is global then you don't get to use those variables. http://php.net/manual/en/reserved.variables.globals.php

Link to comment
Share on other sites

But what if I use only outside functions, it's same.. not defined variable
func_require('/controller/'.$page.'/'.$option.'.php');func_require('/view/default/'.$page.'/'.$option.'.tpl');

When we talk about scope, we're talking about the variables defined in
'/controller/'.$page.'/'.$option.'.php'

being available only within func_require(). And anyway, why do you need a function for that to begin with? What else is this function doing besides doing a "require" on the file?

Link to comment
Share on other sites

Well, it's just including file with $_SERVER['DOCUMENT_ROOT'] Maybe it's unnecessary but whatever.. I have avoided functions func_controller and func_template so I only use func_require..and there's no need to do global $something; but still returns null

Link to comment
Share on other sites

But what is point of this. // file1.php $something = 'value'; // file2.php include 'file1.php'; echo $something; // not defined Is that issue with my code, or what? What to do in this case..

Link to comment
Share on other sites

That case doesn't happen, if you set up those 2 files just like you wrote out you'll find that the variable is defined. Including a file is just like copying and pasting the code in place of the include statement, so it's the exact same as this: $something = 'value';echo $something; Obviously, that variable is going to be defined.

Link to comment
Share on other sites

See, that second example in post 14 is different than the first example in post 12. The second example has a function call. That's why it doesn't work. The function call changes the variable scope. This is what we've told you through the entire thread. Like you said, it's basic PHP knowledge.

Link to comment
Share on other sites

Replace

func_require('/controller/'.$page.'/'.$option.'.php');

with

require $_SERVER['DOCUMENT_ROOT'] . '/controller/'.$page.'/'.$option.'.php';

That is all.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...