How to make a PHP variable available in all pages - php

I recently took a dive into wordpress, and I noticed something really unusual, When coding I noticed that a particular variable called the $post variable was available for me to manipulate whenever I need it, as along as my page is within the wp-includes, wp-themes or wp-plugins folder without me calling any external page or function.
So I started developing a site without wordpress hoping to understand the mystery behind that anomaly..
I would appreciate all help on making me understand this phenomenon. I would like to use such technique in building sites. Thanks...

That's not an anomaly. That variable is present in global scope and is being defined in either of the files that you have mentioned. You can easily do it like
include.php
<?php
$myGlobal="Testing";
?>
anyfile.php
<?php
include "include.php";
echo $myGlobal;
?>
And you can use it in your functions as well, as long as you refer to the global one, for example
anotherfile.php
<?php
include "include.php";
function test()
{
global $myGlobal;
echo $myGlobal;
}
test();
?>
Theory
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well
By declaring (a variable) global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.
Go through this PHP Doc once and you will have much better idea of how it all works.

Take a look at global variables:
http://php.net/manual/en/language.variables.scope.php
and superglobal as well:
http://www.php.net/manual/en/language.variables.superglobals.php

php.ini
register_globals = on
$post
$get
will be available anywhere

Related

PHP:Defing a global scope variable

I am in a situation .
My php folder structure is like
UI
user
login.php
logout.php
jquery
somejs
css
somecss
blah.php
blah.php
Now to import any css ,js or any php file i am using the file path like
localhost/UI/user/index.php // example
Now i am trying to define a global variable on any page like
<?php
$somevar = "localhost";
GLOBAL $somevar;
?>
So that i could import any css js like
<?php echo $somevar ;?>/UI/user/index.php // example
Problem : It is working on that page only where i declared the variable as GLOBAL
I want to use the variable on each page and don't want to use include
Is there any other alternative to define a variable for files folder in php ?
You do not declare variable with global. You just make it available within you method or function body even it was set (or using your terminology "declared") outside of it. So there's no way to have the variable unless it is declared. And there's no way to do that without running the PHP code (simplification, but it does not matter here). And code is not coming from nowhere, hence the need of include or require of said code that sets variable.
You may try to use php.ini's auto_include_file to have your variables auto-included, but still, the PHP code needs to be used for that.
But you generally doing it wrong. move all global variables into class, set autoloader and access i.e. statically. The code will be much cleaner.
Well, my experience says .. global is used for accesing variables inside the function those which defined outside the function.
There can be many different solutions to the same, one i will suggest is use sessions or cookies. Store the data in session / cookies and access it across wherever required.
You have to use define() function for this purpose
config.php
<?php
define("host", "localhost");
?>
now include this page wherever you want to access HOST variable.
you can access this variable like this echo host;
You can use the special PHP-defined $GLOBALS array. The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal
You may not need to define a global variable to apply the css, js or any static files viz Images etc to your application. you may use relative path to include js and css files. You can refer to this article to know more about relative and absolute path.

Included pages receive variables? [duplicate]

This question already has answers here:
Passing PHP variables to an included file?
(4 answers)
Closed 8 years ago.
In my script, I choose which page to include based on a variable.
Does the included page receive the variables that are defined in the main page? Or so I have to redefine them?
If so, what's the best way to pass the variables to the included page?
I tried include("page.php?var=".$var)
But it seems that actually tries to include a file with that string name.
Advice?
If you define you variable before include page, you don't need any query string. Your variable will be accessed in the included page with just name. for example
$name = "Awais"
include("page.php");
then in page.php
echo $name; //will print Awais
Variables that are already in scope in the first page are already defined in the second.
You are better off setting the variables themselves in the main page. include tries to include a local file, not a HTTP GET request, but just set the variables anyway and you can use them.
If you define $var = 1 and after that include("page.php"); the variable will be accessible in that file, since it's nothing more then an extension of what you already got.
This ... "include("page.php?var=".$var)" won't work
Instead try the following:
page1.php
<?php
$dog_name = "scruff";
include("otherpage.php");
?>
otherpage.php
<?php
echo $dog_name;
?>
This will output on page1.php:
scruff
As midnightlightning said: "Variables that are already in scope in the first page are already defined in the second."
Does the included page receive the variables that are defined in the main page?
Yes, the code you include is within the same scope. That is also the documented behaviour, see include.
$var = 'value';
include('page.php'); # has $var defined now.
unset($var);
include('page.php'); # has $var undefined now.
So as you can see, there is no need to redfine them.
But you might want to separate that because it has side-effects, see:
Is include()/require() with “side effects” a bad practice?

PHP variable scope between various template files

This is a newbie question, and I know it.
Template structure is your usual index.php, with a few require_once()'s for the header/footer etc.
I define a var at the top of index.php before any of the require_once()'s for the base url, such as $url = 'http://url';
I then want to echo this out into all template files, header/index/footer etc, it works inside index.php as expected, but fails with a undefined var in all template files that are included in.
I know it's a var scope issue, but I'm totally perplexed how to fix it.
I'm aware that the manual says vars are available to included files, however they aren't. Could it be a issue with my local PHP install?
edit : Created a couple of test files, and a var is defined between 2 files, so why are they not working on my main site files?
Any helps gracefully recieved.
Many Thanks
if you use functions or methods (functions in classes) then you need to do global $variable inside the function. Otherwise you will not have access to it, you also could define it as constant. A constant is always global.
define('MYURL', $url);
You might want to use a PHP framework, if you not already do so.

Wordpress php page $GLOBALS problem

Hi I've an issue while dealing with a php code inside Wordpress;
I've my aaa.php file wich contains code:
<?php
require_once("lang_file.php");
echo $GLOBALS['general']['username'];
?>
My lang_file.php contains:
<?php
$language['general']['username'] = 'User';
?>
And my Wordpress page contains this:
<?php
include("aaa.php");
?>
If i access directly aaa.php through browser i get the "User" message from the echo on aaa.php.
If i access the Wordpress page with include code it doesn't show nothing. I've already read this answer: Does WordPress clear $GLOBALS?
And i tried to define the variables on lang_file.php as $GLOBALS but this still don't work.
You'd need to use
$GLOBALS['language']['general']['username']
instead.
In PHP, $GLOBALS is an array of all variables defined globally. The first element of the array is the global variable name.
Therefore, to access the global variable $language via $GLOBALS, you would need to use $GLOBALS['language']. You can then append any array structure after that which you want to reference from $language.
You can also access it directly via the name $language if you prefer, by adding global $language; to the code prior to where you want to use it.

PHP: Pass a variable to a file you are including?

I am wondering if there is a way to pass a variable to a file you are including via include()?
I tried this but got an error:
include("header_alt.php?img=hey");
Is there a way to do that?
Thanks!
Just define your variable in the first file ; for instance, in temp.php :
<?php
$my_var = 10;
include 'temp-2.php';
die;
?>
And use it in the second file ; temp-2.php :
<?php
var_dump($my_var);
?>
And it should work : I'm getting this output, from temp-2.php :
int 10
The query-string syntax, using stuff like ?img=hey is used when you are requesting some data from a distant server (like when you are using your browser to surf on websites), not when including a file that is on the same server.
Smarty provides a really good mechanism for this sort of thing. Plus, using Smarty just makes for better php applications.
http://www.smarty.net/manual/en/language.function.include.php
Variables can be passed to included
templates as attributes. Any variables
explicitly passed to an included
template are only available within the
scope of the included file. Attribute
variables override current template
variables, in the case when they are
named the same.
All assigned variable values are
restored after the scope of the
included template is left. This means
you can use all variables from the
including template inside the included
template. But changes to variables
inside the included template are not
visible inside the including template
after the {include} statement.

Categories