Access an array defined in an included file from the parent script - php

I'm looking for a way to access an array defined in an included PHP file, from outside this file. I've been Googling on that for a few hours but I didn't really find anything that answered my question.
I've tried using $GLOBAL['varname'] outside my script after using global $varname in the included file but it doesn't seem to work, got to say I'm a bit confused.
Is there any way I can do this in PHP?
Thanks a lot for the answer!

user $_SESSION variables. so you will be able to access your variables anywhere in your php application.. untill you unset it..
try this..
session_start();
$_SESSION['array'] = $array; //your array
now you can use this variable/array in any file of your project..
print_r($_SESSION['array']);

if you include a file in PHP you can access the variable the same way you would if it was in the same file..
File1 (foo.php):
<?php
$var = array('foo','bar');
File2:
<?php
require_once('foo.php');
print_r($var);
does this not work..?

Related

How do I call a PHP function with GET parameters [duplicate]

So basically I have a file, lets say file1.php and it includes another file2.php.
file1.php contains a variable called $dateString that I have performed some operations on.
file2.php also has the same variable called $dateString however this variable is not being referenced within this include file even though $dateString was initialized prior to including file2.php.
So this is currently how file1.php looks like:
$dateString = 'some expression';
include 'file2.php';
file2.php is basically trying to echo out the variable that was initialized in file1.php
echo $dateString;
Everything else from file2.php is being called properly except that one variable.
Hopefully this makes sense. Is this possible in PHP? I have read in other places that you need to make the variables global and others have mentioned how you cant reference a variable from a parent file within a child file..any thoughts? Thanks in advance!
there is nothing magically happening when you use PHP's include(). the included file's code is basically just inserted at the point where you are including it - resulting in one bigger PHP file.
it doesn't really matter if the "parent" or in the "child" file - it's the position that matters since PHP will parse the single resulting file from top to bottom.
for example: if you set the variable in file A before including file B, file B will know about the variable. but if you set the variable in file A after including file B - file B doesn't know it.
With coding you should always try and test your own things before asking other people, butch like what #bub suggested in the comments.
Variables are global pretty much, you are able to include another file and use variables that are in file1.php in file2.php.
Otherwise, if you are using them in a function, I'd suggest using:
global $var;
the above should only be used in a function.

How to make a PHP variable available in all pages

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

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 object property NULL in required page

Why does this work:
echo $session->name;
require_once('test.php');
//echos the name then loads the required page
But
var_dump($session->name);
in the code of test.php returns NULL.
I thought require_once pretty much plops all the code from the required page into the spot I told it to.
EDIT
$session instance is created before the require, outside of test.php. If I create a new instance of $session inside test.php it works. Glad it works but this still doesnt make sense to me, can anyone explain?
$session is not in the scope. include that script where you have initialized $session. or declare it as global in all scripts that will save the include just for $session. why not use $_SESSION though?
Dumb question: Have you already require_once'd test.php somewhere else in your script? If so, then it's possible that $session may not have been set when you included it the first time. Have you tried include('test.php') instead?
DEBUGGING EDIT:
You can validate the order things are being called in by trying the following:
In the caller.php (or whatever you call it) file:
echo "Debug 1";
echo $session->name;
require_once('test.php');
In test.php:
echo "Debug 2";
var_dump($session->name);
Which is printed first, Debug 1 or Debug 2?

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.

Categories