0

How would I pass a global var in two external scripts?

<div>
<!-- INCLUDEPHP 1.php -->
</div>
<div>
<!-- INCLUDEPHP 2.php -->
</div>

I have tried creating global variables on 1.php and `2.phpp but it didn't work.

1.php:

<?php
global $someVar;
$sql = ...;
$someVar= $db -> sql_query($sql);
?>

2.php:

<?php
global $someVar;
echo "$someVar";
?>

Am I doing something wrong?

10
  • 3
    what's the include code, http? or ? Commented Jun 10, 2012 at 20:45
  • 1
    I don't think you should not be using Global variables(not good practice). You could potentially use $_SESSION, to undertake what you require though. Commented Jun 10, 2012 at 20:46
  • 1
    @Haroon: NO NO NO NO NO! Don't use session just to communicate between scripts when building a page! Commented Jun 10, 2012 at 20:47
  • its http code. INCLUDE '1.php' ; Commented Jun 10, 2012 at 20:48
  • 1
    @wtsang02 can you give the exact code, im a little confused, if its local a normal var will work in an included file. Commented Jun 10, 2012 at 20:50

2 Answers 2

2

I would try including the scripts via PHP:

<div>
<?php require "1.php" ?>
</div>
<div>
<?php require "2.php" ?>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

@wtsang02: What were you doing before‽ Was <!-- INCLUDEPHP 2.php --> your actual code?
so the issue was, the syntax for include, noting to do with variables?
@Eric Not enough global -1 ;-)
when including scripts, require_once is preferable to check if the file has been already included
1

If both includes are loaded into the same page, and the variables exist already in the global scope, all your functions can access them with the global statement. Since everything is already global, the statement is not required in the global scope, only inside functions. This also permits functions to share variables by casting them onto the global scope.

There are many dangers to this, though, which I'll not pretend to be fully aware of, so one is best advised to make prudent use of the global scope in large complex applications as they can become very volatile if naming conventions are relaxed.

Basically, we're looking at,

function arrow() { global $a; $a = "arrow"; return $a; }
function sky() { global $b; $b = "sky"; return $b; }
echo "I shot an " . arrow() . " into the " . sky() . ".";
echo "I shot an $a into the $b.";

which is child's play, it demonstrates how exposed the variables are, sitting out there with no protection. Now another function can come along and blow the whole thing apart:

function whammo() { global $a, $b; $c = $a; $a = $b; $b = $c;}
echo "I shot an " . arrow() . " into the " . sky() . ".";
whammo();
echo "I shot an $a into the $b.";

See what I mean?

Your solution probably lies in a closure of some sort, wherein all the functions are contained that need this 'global'. It will be much better protected.

Comments

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.