Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a variable across two files

I have three files - global.php, test.php, test1.php

Global.php

$filename;
$filename = "test";

test.php

$filename = "myfile.jpg";
echo $filename;

test1.php

echo $filename;

I can read this variable from both test and test1 files by include 'global.php';

Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.

I tried with session variables as well but due to two different files i am not able to capture the variable.

How to achieve this........

Thanks for help in advance.....

like image 395
user2688512 Avatar asked Sep 03 '13 09:09

user2688512


People also ask

How do you use an extern variable in multiple files?

extern int globalVar; When you use extern keyword before the global variable declaration, the compiler understands you want to access a variable being defined in another program or file, and hence not to allocate any memory for this one. Instead, it simply points to the global variable defined in the other file.

Can global variables be used in different files?

Initialization — if a global variable is declared in more than one source file in a library, it should be initialized in only one place or you will get a compiler error. Static — use the static keyword to make a global variable visible only to functions within the same source file whenever possible.

How do I use extern to share variables between source files?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.


3 Answers

Use:

global.php

<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
    $_SESSION['filename'] = $filename;
}
?>

test.php

<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>

test1.php

<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
like image 179
MaxEcho Avatar answered Oct 03 '22 03:10

MaxEcho


First you start session at the top of the page.

Assign your variable into your session.

Check this and Try it your self

test.php

<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>

test1.php

<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>
like image 25
Padmanathan J Avatar answered Oct 03 '22 04:10

Padmanathan J


I think the best way to understand this is as following:
You have one file index.php whit some variable defined.

$indexVar = 'sometext';

This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.

You should redefine the scope of this variable in your new file, like this:

global $indexVar;

Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.

like image 20
Sky Games Inc Avatar answered Oct 03 '22 04:10

Sky Games Inc