0

I'm using wordpress, and I have some variables that I need to use on some other pages, these variables are on different files that are included on the index (<?php include('somefile.php');?>).

My question is the variables from somefile.php can be echoed directly on index.php ? If yes can you tell me how ?

Thanks in advance

1
  • generaly yes, bu it's too generous question, what's the content of somefile.php ? why don't you include it all? Commented Aug 8, 2011 at 2:36

1 Answer 1

3

tl;dr

Yes

<?php echo $someVariable ?>

Wordpress is notorious for loading everything and the kitchen sink into the global space. What this means for you is that generally any variable defined in an included file that is not within a function or method can be used just about anywhere else.

The thing you have to be careful of is conflicts or overrides. For instance, I wouldn't declare any variables named $wp or $wpdb.

Easiest thing to do is here come up with a pseudo-namespacing system. For example, prefixing your classes, global variables, etc with your name / company name is a good approach, eg

// somefile.php
$daniel_somevariable = 'foo';
Sign up to request clarification or add additional context in comments.

1 Comment

Just to discuss: is it a good habit to have a trailing ";" after the echo statement , i.e. <?php echo $someVariable; ?> ?

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.