Jump to content

Need help understanding the scope of variables


Recommended Posts

I need a bit of help understanding the scope of variables

 

If I have the following code:

 

<?php
$myvar = true;

include(otherfile.php);
?>

 

Is $myvar available for access as is in the code from otherfile.php?

 

Specifically though, I'm trying to get my head round some Wordpress coding, so I have this instead:

 

 

<?php
$myvar = true;

<?php get_header(); ?>
?>

 

<?php get_header(); ?> puts the contents of header.php at this location + I'm sure it does some other things like error checking etc

 

What is the best way to have $myvar available?

The ONLY thing is I only want $myvar = true just for one file - everywhere else it should be false or not exist

 

I'm wary of using a global variable - but if it's the only way, then I have to

 

Thanks

 

 

Omar

 

$myvar would equal "true" in the page where it was given the value "true" AND in the included page otherfile.php.  In all other pages, $myvar would equal "" unless it was set to some other value.  I think that's what you want? 

 

You would probably get a php warning in the 'other' pages if $myvar was referenced and not initialized.

 

Or, I may not understand your question.  In which case I apologize.

thanks for confirming

 

the problem is with this version:

 

<?php
$myvar = true;

<?php get_header(); ?>
?>

$myvar doesn't seem available in header.php - get_header() puts the code of the file in the place, so i thought similar to the above the variable would be available

 

i'm sure i'm doing something really simple wrong?

Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. 

 

In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include)

 

In your second example, $myvar will not be available in get_header() because functions don't access the global scope.  If you wish $myvar to be available in get_header you'll have to pass it in.

 

(It should be noted that your second example is a PHP fatal parse error).

 

The manual page for variable scope will tell you more.

 

-Dan

Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. 

 

In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include)

 

In your second example, $myvar will not be available in get_header() because functions don't access the global scope.  If you wish $myvar to be available in get_header you'll have to pass it in.

 

(It should be noted that your second example is a PHP fatal parse error).

 

The manual page for variable scope will tell you more.

 

-Dan

thanks dan, that makes more sense

the fact that get_header() spits out the contents of header.php - does not mean it has access to variables in other files

 

in that case, i only have the choice of using a global variable?

Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. 

 

In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include)

 

In your second example, $myvar will not be available in get_header() because functions don't access the global scope.  If you wish $myvar to be available in get_header you'll have to pass it in.

 

(It should be noted that your second example is a PHP fatal parse error).

 

The manual page for variable scope will tell you more.

 

-Dan

thanks dan, that makes more sense

the fact that get_header() spits out the contents of header.php - does not mean it has access to variables in other files

 

in that case, i only have the choice of using a global variable?

 

Ideally, you'd simply modify the function to accept your $myvar variable as a parameter:

 

function get_header($arg = null)
{
   if (isset($arg))
   {
      // do something with $arg
   }

   // continue as normal
}

// later in your code

$myvar = "something";

get_header($myvar);

so you're saying there's a wordpress FUNCTION called get_header() that uses the CODE inside header.php?

 

The wordpress function obviously include()s the header.php file inside itself.  This is bad practice (because, shockingly, it makes scope very confusing) but wordpress is full of bad practices. 

 

If you really need to include a variable from one page into the header.php there must be a way to inject it into that function in some way.  Can you pass it an array or something? 

 

Failing that, use this syntax inside header.php:

global $myvariable;

 

Note that it's wrong to do that, but you're working within the restrictions of a relatively terrible piece of software.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.