Asked  7 Months ago    Answers:  5   Viewed   54 times

I'm trying to pass a variable from one include file to another. This is NOT working unless I declare the variable as global in the second include file. However, I do NOT need to declare it as global in the file that is calling the first include. For example:

$name = 'james';


echo $name;

output: james

echo $name;

output: nothing

IF I declare global $name prior to echoing $name in, then it works properly. The accepted answer to this post explains that this depends on your server configuration: Passing variables in PHP from one file to another

I'm using an Apache server. How would I configure it so that declaring $name to be global is not necessary? Are there advantages/disadvantages to one vs. the other?



When including files in PHP, it acts like the code exists within the file they are being included from. Imagine copy and pasting the code from within each of your included files directly into your index.php. That is how PHP works with includes.

So, in your example, since you've set a variable called $name in your file, and then included both and in your index.php, you will be able to echo the variable $name anywhere after the include of within your index.php. Again, PHP processes your index.php as if the code from the two files you are including are part of the file.

When you place an echo within an included file, to a variable that is not defined within itself, you're not going to get a result because it is treated separately then any other included file.

In other words, to do the behavior you're expecting, you will need to define it as a global.

Wednesday, March 31, 2021
answered 7 Months ago

Something like this

<script type="text/javascript">
var markers = <?php echo json_encode($row) ?>;

Now the markers JavaScript variable will be available to any scripts following the above.

You can access associative entries from the PHP $row array using

var latlng = markers.latlng;

While we're here, you should protect your query from SQL injection. Whilst I always recommend using PDO and parameter binding, a quick fix for you would be

$ref = get_magic_quotes_gpc() ? stripslashes($_GET['ref']) : $_GET['ref'];
$sql = sprintf("SELECT * FROM `markers` WHERE `ref` = '%s'",
Wednesday, March 31, 2021
answered 7 Months ago

I answer this question:

will the echoed $variable1 be displayed as what the respective users have input to their browser or it will override the first user's input by the last user's input?

The echoed $variable1 will be displayed as what the respective users have input to their browser.

PHP makes thread per request, so different requests have different variables (include global).

I check this post.

As said by JasonB, PHP global variable has a very wide scope, and make code complex.

Wednesday, March 31, 2021
answered 7 Months ago

Change it

 $image = '$_GET[image_url]'; 


 $image = $_GET['image']; 
Saturday, May 29, 2021
answered 5 Months ago

So as I guessed you are using a framework as you said in the comments:

@Rizier123 Yes, I'm using Laravel. Does it matter? – Kai 6 mins ago

And if it matters? Yes it does.

Probably what is happening here is, that the code which you show us here is wrapped into another function somewhere else.

Means that the variables in the Sum() function are in global scope, but the other ones outside of it not, since they are probably in another function == another scope.

You can reproduce it with this code:

function anotherFunction() {
    $a = 1;
    $b = 2;

    function Sum() {
        $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];

    echo $b;


And if you have error reporting on you will get:

Notice: Undefined index: a
Notice: Undefined index: b

Just put the error reporting at the top of your file(s) to get useful error messages:

    ini_set("display_errors", 1);

To solve this now you have to declare the variables also in global scope, either with:

$GLOBALS["a"] = 1;
$GLOBALS["b"] = 2;

or like this:

global $a, $b;
$a = 1;
$b = 2;
Saturday, July 31, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :